Solved

PHP inside of JQuery

Posted on 2014-02-25
8
444 Views
Last Modified: 2014-02-26
How do I place PHP inside of JQuery?

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');
		}
	});

Open in new window


I need to know how to write the <?php echo $id; ?> Part.
0
Comment
Question by:rgranlund
8 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39887391
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39887394
Does what you posted not work?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39887402
Nothing wrong with the way you have it but that javascript must be in your php page, you cannot have it in an external js file.
For things like this I tend to drop an hidden text field in the php page and call that field from the js file so I can keep everything separate.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39887438
Differences between client and server applications are explained in this article.
http://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.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39887464
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>

Open in new window

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>\")";
?>

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39887607
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:

<script>var id = <?=$id ?></script>
<script type='tex/javascript' src='your_script.js'></script>

Open in new window


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');
		}
	});
                                  

Open in new window


It works fine but I'm not sure if this is a good practice...
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39887717
I like that too, I'll probably use that somewhere.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39887718
@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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php string detection problem 7 34
tutorial for ebay api 3 35
does post require a form or curl to be post 4 35
How can I make this form submit to itself? 10 28
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question