sayedsohail Posted March 15, 2007 Share Posted March 15, 2007 Here is my script, I am tryin to pass php value to javascript function. <tr id="action" title="click to do something" onclick="passvar(<?php print '$pageNum';?>);"> ?> function passvar(pan) { var page = <?php echo($pageNum);?>; var bage = pan; alert("Your user ID is:" + page); alert("Your user ID is:" + bage); } Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/ Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 I do not think it is possible without the use of AJAX, I would look that up via www.google.com Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208278 Share on other sites More sharing options...
obsidian Posted March 15, 2007 Share Posted March 15, 2007 Here is my script, I am tryin to pass php value to javascript function. <tr id="action" title="click to do something" onclick="passvar(<?php print '$pageNum';?>);"> Your problem is in the fact you are echoing your PHP variable within single quotes so it is literally printing out "$pageNum" instead of the value of the variable. Just change it to this, and you should be fine: onclick="passvar(<?php print $pageNum;?>);" @frost110 - Remember that PHP sending values to javascript can happen all day long, but you cannot communicate back to PHP from javascript without the use of HTTP requests (AJAX). Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208286 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 Yea, my bad. I read what he wanted wrong. =) Nice catch ob. Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208287 Share on other sites More sharing options...
sayedsohail Posted March 15, 2007 Author Share Posted March 15, 2007 Here is the revised code, still it doesn't work. <tr id="action" title="click to do something" onclick="passvar(<?php print $pageNum;?>);"> function passvar(pan) { var bage = pan; alert("Your user ID is:" + num); //onclick="document.location.href='foo.html' } Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208291 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 <?php $pageNum = 5; ?> <tr id="action" title="click to do something" onclick="passvar(<?=$pageNum;?>);"> function passvar(pan) { var bage = pan; alert("Your user ID is:" + num); //onclick="document.location.href='foo.html' } Make sure $pageNum has a value. Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208292 Share on other sites More sharing options...
obsidian Posted March 15, 2007 Share Posted March 15, 2007 Here is the revised code, still it doesn't work. Is that your entire script? In that example, you are trying to apply an onclick handler to a DOM object that is not even complete. Can you give us an example of what you're after, and we'll be more than happy to try to help you work through it. Also, while frost110 has the gist of things, try to avoid the "<?=" opening PHP tag since it is deprecated. Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208294 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 It is depreciated, damn that is good to know. I havn't worked much with PHP 5, but I will avoid that now. Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208297 Share on other sites More sharing options...
obsidian Posted March 15, 2007 Share Posted March 15, 2007 You have a couple other serious issues with your javascript: 1) You need to try to declare javascript functions before you make a call to them 2) You need to reference the variable you have assigned (in your case, you are assigning the variable "bage" and then trying to alert with variable "num" that doesn't exist. Here's a full sample page that's tested and works: <?php $myVar = 5; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script type="text/javascript"> function passvar(pan) { var bage = pan; alert("Your user ID is: " + bage); } </script> </head> <body> <div onclick="passvar('<?php echo $myVar; ?>')">My div</div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208300 Share on other sites More sharing options...
sayedsohail Posted March 15, 2007 Author Share Posted March 15, 2007 thank you guys it works now. actually its a very long script just for your info i am display two tables with pagination on one page and am trying to update the second table rows when a user click on any of the table1 row. thus a feel of master detail relationship data. Link to comment https://forums.phpfreaks.com/topic/42884-solved-pass-php-value-to-javascript-please-help/#findComment-208301 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.