rain13 Posted March 18, 2011 Share Posted March 18, 2011 From javascript I call func as follows <?php echo hello(i); ?>. Now problem is that hello prints me i, not number. I should be number assigned by javascript. <?php $var = "Hello";function hello($i){echo "Rating: ".$i;}?><html><head> <title>Javascript Change Image Onmouseover</title> <style type="text/css"> p { font-family:Verdana; font-weight:bold; font-size:11px } img { cursor:pointer; } </style> <script language="javascript" type="text/javascript"> function mouseOverImage(n) { document.getElementById("txt").innerHTML = n; for (i=1;i<=n;i++) { document.getElementById("img"+i).src = "0.png"; } } function mouseOutImage() { document.getElementById("txt").innerHTML = "rate"; for (i=5;i>=1;i--) { document.getElementById("img"+i).src = "1.png"; } } function Rate(i) { document.getElementById("txt").innerHTML = '<?php echo hello(i); ?>'; } </script></head><body><table style="text-align: left;" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td><img id="img1" src="1.png" width="24" height="24" alt="image rollover" onmouseover="mouseOverImage(1)" onmouseout="mouseOutImage(5)" onmousedown="Rate(1)"/><img id="img2" src="1.png" width="24" height="24" alt="image rollover" onmouseover="mouseOverImage(2)" onmouseout="mouseOutImage(4)" onmousedown="Rate(2)"/><img id="img3" src="1.png" width="24" height="24" alt="image rollover" onmouseover="mouseOverImage(3)" onmouseout="mouseOutImage(3)" onmousedown="Rate(3)"/><img id="img4" src="1.png" width="24" height="24" alt="image rollover" onmouseover="mouseOverImage(4)" onmouseout="mouseOutImage(2)" onmousedown="Rate(4)"/><img id="img5" src="1.png" width="24" height="24" alt="image rollover" onmouseover="mouseOverImage(5)" onmouseout="mouseOutImage(1)" onmousedown="Rate(5)"/></td> </tr> <tr> <td id="txt"> rate </td> </tr> </tbody></table></body></html> Link to comment Share on other sites More sharing options...
ShadowMage Posted March 18, 2011 Share Posted March 18, 2011 echo hello($i);Without the $ it is assuming the string 'i' rather than looking for the variable $iEDIT:Wait...are you trying to pass the variable i from the JavaScript Rate function to PHP? That can't be done. JavaScript and PHP run in two entirely different environments (client vs. server) and the only way to communicate between the two is through AJAX. Link to comment Share on other sites More sharing options...
rain13 Posted March 18, 2011 Author Share Posted March 18, 2011 How do I write ajax code for it? I want to send argument i from Rate function to server, and then get server's reply. Link to comment Share on other sites More sharing options...
ShadowMage Posted March 18, 2011 Share Posted March 18, 2011 It depends on what exactly it is you're trying to do with the Rate function. You could send an AJAX request to the server with the rating and perform whatever operations you need. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.