I'm having issue when echoing alert() in php. It show everything in the string.
echo "<script type='text/javascript'>alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');</script>";
Output
You can use javascript code between ending and starting php tags like below,
?>
<script type="text/javascript">
alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');
</script>
<?php
// rest of the php code will go here.
?>
OR try this please,
<?php
echo '<script type="text/javascript">';
echo 'alert("Thank you for your enquiry, we will contact you as soon as possible, have a nice day!");';
echo '</script>';
?>
Solution: I fixed it by removing everything. and just echo the message directly without the alert(). I'm not sure why it behave like this because this is my custom plugin. I will update if I found the source of this behavior. Thank you for all the replies.
echo 'Email Successfully Sent. We will reply soon!';
Related
For some reason this code doesn't work. Adding \ to the front of the $ doesn't change anything. Can anyone see the problem?
$commentid = 2; //for example
echo "<script>";
echo "<div id = 'commentinput".$commentid."'>";
echo "</div>";
echo "$('#commentinput".$commentid."').load('editcomments2.php')";
echo "</script>";
Do it like this:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
I'd suggest not using echo to output HTML (in most cases anyway), but closing the PHP tag instead.
This becomes (edited with a loop example, see comments below):
<?php while ($commentid = $dbObject->dbMethod()): ?>
<div id="commentinput<?= $commentid ?>">
</div>
<script>
$('#commentinput<?= $commentid ?>').load('editcomments2.php');
</script>
<?php endwhile ?>
Note that in this case, the <script> portion should probably be outside of the loop, with a jQuery selector matching all the comment inputs.
So besides all of the other answers, I found another solution to!
When echoing out js code, like
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
//and
echo "alert('stuff')";
it might then print it out like: $("#commentinput2").load("editcomments2.php")alert('stuff') and the code will get confused. So lastly I need to added a semicolon to prevent the program from reading all my code as just 1 line:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
I have a "Bootstrap" form. When the user press "Submit" data is sent to file.php If the database is updated, I have an alert in that php file which echos an alert with "success". Now I'd like to alert a timed message. Not the alertbox with "OK" button. Is there some small simple code for this?
This is what I have now!
PHP
//Echo succes
echo "<script type='text/javascript'>";
echo "alert('Välkommen ".$row['usr_fname']." ".$row['usr_lname']."');";
echo 'window.location = "../back_to_form.html"';
echo "</script>";
die();
You could potentially use something like Jquery UI to fake a dialog box. Then the reloading could happen just like you want it to. The code would look something like this (untested, you'd need to adapt it to your app):
echo "<html><body>";
echo '<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>';
echo "<div id="dialog" title="Basic dialog">";
echo "<p>Välkommen ".$row['usr_fname']." ".$row['usr_lname']."</p>";
echo "</div>";
echo "<script type='text/javascript'>window.settimeout(window.reload(), 5000)</script>";
echo "</body></html>";
More info with a better javascript example for the jquery ui message box can be found here
I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})
I'm trying to make the page go back when the user presses the OK button on the alert box. The alert box appears but it doesn't go back to the previous page
Would i have to use a confirm window because i don't want to provide the cancel option, i just want a window to appear so the user has to click OK and it goes back to the previous page
print '<script type="text/javascript">';
print 'alert("Invalid please try again.")';
print 'history.back();';
print '</script>';
Just to provide a little more information I'm doing this after OR
$link =mysqli_connect(*sensitive info*) OR print '<script type="text/javascript">';
print 'alert("Invalid please try again.")';
print 'history.back();';
print '</script>';
exit();
Try this, You have missed to add ;
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
.........^
print 'history.back();';
print '</script>';
I think you're missing a semicolon ; after alert().
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
print 'history.back();';
print '</script>';
You have missed the ';' after your alert
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
print 'history.back();';
print '</script>';
To find our errors in code you can use websites such as http://www.jslint.com/ to catch these errors easily.
echo "<script type=\"text/javascript\">";
echo "alert(\"Invalid please try again.\")";
echo "window.location= \"/*Write ur page Link Here*/\"";
echo "</script>";
Hope above code will work for you. and yeah ";" is missing after your alert line.
My question is very simple. All I want to do is write actual PHP code in HTML, without executing it. For example, I want to write
<div id = "code"><?php
echo 'Bla Bla Bla';
?></div>
I DO NOT want to execute this PHP. I want to literally write the PHP code on the html document, by somehow escaping the <?php tags (I don't want to remove them).
Unfortunately, when I echo it to the browser by doing this:
<div id = "code"><?php echo '<?php
echo "Bla Bla Bla";
?>' ?></div>
I have also tried to put the code into variables, and even that hasn't worked.
My PHP code gets commented out by the browser. I can see it in the View Page Source option in chrome, but the browser thinks it's a comment.
Please provide a solution to my problem.
Thanks in advance.
PS: I am using CodeMirror to output embedded PHP/HTML code using the 'application/x-httpd-php'.
EDIT #1
I can use lt and rt for tags, but I actually can't because CodeMirror starts highlighting code only when it finds <? tags.
Encode your PHP tags by using <?php echo "test"; ?>.
Escape your code with htmlspecialchars():
<div id="code">
<?php echo htmlspecialchars("<?php echo 'Bla Bla Bla' ?>") ?>
</div>
Take a look here: http://www.php.net/manual/en/function.htmlentities.php
<div id = "code"><?php echo htmlentities('<?php
echo "Bla Bla Bla";
?>'); ?></div>
Try this
<div id = "code">
<?php echo htmlentities('<?php echo "Bla Bla Bla"; ?>'); ?>
</div>
try using htmlspecialchars
htmlspecialchars()
if i understood your question properly then you can achive it by following..
<div class="test"><?php $a='<?php hii echo "hey there"?>';?></div>
Solution 1: Your solution would probably be to not name the file .php ... Try .txt in stead. This way the web server would not pass the file to the PHP parser.
Solution 2: Other solution would be to read out the file from another php script that does file() in combination with htmlspecialchars() to the file with the desired code.
Solution 3: If you already have the code in a variable and it outputs in a way that you can see the code in "source view", then you suffice by only using htmlspecialchars()