Page 1 of 1
How to embed PHP into Javascript?
Posted: Sun Nov 11, 2007 9:12 pm
by chxxangie
is it possible?
Code: Select all
<SCRIPT LANGUAGE="Javascript">
document.location = "product.php?search=<?php echo $search;?>"
</SCRIPT>
Posted: Sun Nov 11, 2007 9:36 pm
by Josh1billion
Nope, Javascript is client-side and PHP is server-side. You might be able to make PHP script calls with Ajax but I'm not sure as I've never used Ajax.
Looking at your example code, I'm not entirely sure what you're trying to do, but if you had all that within a print function around all that, you could output PHP variables into your Javascript script if that's what you're asking about.
Posted: Sun Nov 11, 2007 11:07 pm
by Kieran Huggins
sure you can do that - give it a try!
Posted: Sun Nov 11, 2007 11:37 pm
by sunilbhatia79
You can do it... ultimately you are just echoing the $search variable to the HTML output... Instead of using <?php echo $search;?> you could also use <?=$search?>. This is much better for maintenance and debugging.
Try this:
Code: Select all
<SCRIPT LANGUAGE="Javascript">
document.location = "product.php?search=<?=$search;?>"
</SCRIPT>
Posted: Mon Nov 12, 2007 12:03 am
by Kieran Huggins
careful about using short tags, they'll be turned off by default soon.
Posted: Mon Nov 12, 2007 4:41 am
by onion2k
sunilbhatia79 wrote:You can do it... ultimately you are just echoing the $search variable to the HTML output... Instead of using <?php echo $search;?> you could also use <?=$search?>. This is much better for maintenance and debugging.
Care to explain why? I've always found that using shortcuts in code makes them harder to maintain and debug.