Robert Granlund
asked on
PHP inside of JQuery
How do I place PHP inside of JQuery?
I have the following:
I need to know how to write the <?php echo $id; ?> Part.
I have the following:
$(document).ready(function() {
$('input[type="radio"]').click(function() {
if ($(this).attr("value") == "custom yes") {
$("#custom-hidden-<?php echo $id; ?>").show('slow');
}
if ($(this).attr("value") == "custom no") {
$("#custom-hidden").hide('fast');
}
});
I need to know how to write the <?php echo $id; ?> Part.
You would not put your php inside of jquery, but you could wrap the entire piece of code around php where you echo the entire thing.
Does what you posted not work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Differences between client and server applications are explained in this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
Basically, PHP runs on the server and generates the HTML document (including CSS and JavaScript such as jQuery). After PHP has generated the document it sends the document to the client and goes back to sleep to await the next request. The next request may come from a human mouse-click or from a jQuery method, but whatever PHP sent in its initial HTML document is what the browser has to work with unless and until another request is made.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
Basically, PHP runs on the server and generates the HTML document (including CSS and JavaScript such as jQuery). After PHP has generated the document it sends the document to the client and goes back to sleep to await the next request. The next request may come from a human mouse-click or from a jQuery method, but whatever PHP sent in its initial HTML document is what the browser has to work with unless and until another request is made.
On one of my own pages, I have a javascript link that is actually a PHP page that generates the javascript. The javascript link is:
<script type="text/javascript" src="ip.php"></script>
And the PHP page is:<?php
//"ip.php" example- display user IP address on any page
Header("content-type: application/x-javascript");
$remoteIP=$_SERVER['REMOTE_ADDR'];
echo "document.write(\"<div><b>Your IP address is: " . $remoteIP . "</b></div>\")";
?>
I know another way to do that if you have your javascript in an external file:
in the head section of your page you can write:
Then in your script you simply use the javascript variable id:
It works fine but I'm not sure if this is a good practice...
in the head section of your page you can write:
<script>var id = <?=$id ?></script>
<script type='tex/javascript' src='your_script.js'></script>
Then in your script you simply use the javascript variable id:
$(document).ready(function() {
$('input[type="radio"]').click(function() {
if ($(this).attr("value") == "custom yes") {
$("#custom-hidden-"+id).show('slow');
}
if ($(this).attr("value") == "custom no") {
$("#custom-hidden").hide('fast');
}
});
It works fine but I'm not sure if this is a good practice...
I like that too, I'll probably use that somewhere.
@marqusG: I don't see anything wrong at all -- it makes perfect sense to me. I point out the client/server difference because sometimes there is confusion about what happens first and next, what happens on the server then on the client. Both your example and Dave's example seem quite feasible. At least that's the way I see it.
Best to all, ~Ray
Best to all, ~Ray