I Have A problem In displaying JS Code In PHP My Code Is
<?php
if($result)
{
echo '<script language="javascript">';
echo "$('#Err').hide()</script>";
}
else
{
echo '<script language="javascript">';
echo "$('#Err').show()</script>";
}
?>
I Am Using XAMPP For running Code When I Run This Code It Will Not Display I Don't know what's the problem plz tell me Thanks!
-
The way you have written your code tells me you don't know about escaping quotes in strings. If you don't know such a basic thing, I'm not hopeful that you tried View Source to see what PHP is outputting.Niet the Dark Absol– Niet the Dark Absol2012年05月05日 06:12:40 +00:00Commented May 5, 2012 at 6:12
-
When you 'view source' of the resulting page in the browser is the <script> tag in there at all and just not working like you expect or is it completely missing?nvuono– nvuono2012年05月05日 06:13:18 +00:00Commented May 5, 2012 at 6:13
-
The script tag is there for me in FF12, and I get a JS error, "$ is not defined." Have you included your jQuery dependency file? Also, if you're not already, load up Firefox and add the 'Firebug' extension, it will make debugging Javascript much easier.danjah– danjah2012年05月05日 06:14:30 +00:00Commented May 5, 2012 at 6:14
-
The given answers are correct. However, my personal preference is to always have static JavaScript that acts on some dynamic bit of data via a hidden field. That way you can easily cache the JS into external files.Matthew– Matthew2012年05月05日 06:18:57 +00:00Commented May 5, 2012 at 6:18
-
I put an answer below. Try not to use echo when throwing html or scripts, it's a good practice for PHP development, and also you won't need to worry about escaping (and so far, hardcoding) other languages.lu1s– lu1s2012年05月05日 06:21:02 +00:00Commented May 5, 2012 at 6:21
5 Answers 5
You have to escape the $ sign as follows:
<?php
if($result)
{
echo '<script language="javascript">';
echo "\$('#Err').hide()</script>";
}
else
{
echo '<script language="javascript">';
echo "\$('#Err').show()</script>";
}
?>
3 Comments
$ it does not have to be escaped. (But your good intentions a clear, just FYI).Use single quotations, your double quotations are rendering $('#Err') as a PHP variable
<?php
if($result)
{
echo '<script language="javascript">';
echo '$(\'#Err\').hide()</script>';
}
else
{
echo '<script language="javascript">';
echo '$(\'#Err\').show()</script>';
}
?>
Comments
<?php
if($result)
{
echo '<script type="text/javascript" language="javascript">
$("#Err").hide()</script>';
}
else
{
echo '<script type="text/javascript" language="javascript">
$("#Err").show()</script>';
}
?>
Comments
This way is more understandable and uses less processing:
<!-- this is your php script with your html layout -->
<script type="text/javascript">
<?php if($result === true): ?>
$("#Err").hide();
<?php else: ?>
$("#Err").show();
<?php endif; ?>
</script>
Try not to use echo. It's a good practice for PHP. If you really want to increase your php productivity, I highly recommend the Codeigniter framework. Give it a try =).
Comments
You need to do some debugging, look into the source of your HTML that get's generated:
<?php
// debug code:
var_dump($result); die('This place has been reached');
// normal code:
printf("<script language=\"javascript\">\n\$('#Err').%s()\n</script>"
, $result ? 'hide' : 'show');
?>
When you execute this, things will go wrong, but you can see what $result contains so to debug your script. If it does not display that inside the HTML source, it means that your code is not executed at all and your problem lies somewhere else.