sayedsohail Posted March 17, 2007 Share Posted March 17, 2007 Hi, I wish to echo some values to a javascript function here is my script. passvar() is my javascript function. Please help. echo '<tr id="action" title="Click to display sites below" onclick="passvar($pageNum,$id,$name,this );">'; Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/ Share on other sites More sharing options...
kenrbnsn Posted March 18, 2007 Share Posted March 18, 2007 You have the string delimited by single quotes. Variables are not expanded inside single quotes. Try this: <?php echo '<tr id="action" title="Click to display sites below" onclick="passvar(' . $pageNum . ',' . $id . ',' . $name . ',this );">'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209678 Share on other sites More sharing options...
sayedsohail Posted March 18, 2007 Author Share Posted March 18, 2007 it work on all integers, but if i pass any string var its not working, any ideas. Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209685 Share on other sites More sharing options...
chawezul Posted March 18, 2007 Share Posted March 18, 2007 You can use <del>single</del> <ins>double</ins> quotes <?php echo "<tr id=\"action\" title=\"Click to display sites below\" onclick=\"passvar(${pageNum}, ${id}, ${name}, this );\">"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209688 Share on other sites More sharing options...
sayedsohail Posted March 18, 2007 Author Share Posted March 18, 2007 $name in my function is a string value, when i add this its not working either with single or double quotes, but for the rest of integer variables it works perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209694 Share on other sites More sharing options...
chawezul Posted March 18, 2007 Share Posted March 18, 2007 Can you supply more of the code? From what I've seen it should be working perfectly, there's gotta be something interfering with $name that I can't see. Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209696 Share on other sites More sharing options...
sayedsohail Posted March 18, 2007 Author Share Posted March 18, 2007 Here is the code, which echo passes four values to the function and the function in turn just alert the values. <?php echo "<tr onclick=\"passvar(${pageNum}, ${id}, ${name}, this );\">"; echo "<td>$id</td<td>$name</td></tr>"; ?> <SCRIPT> here is my javascript function. function passvar(pan,rec,nam,obj) { var bage = pan; var cid = rec; var cname = nam; alert("This is your company name"+cname); } </SCRIPT> Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209702 Share on other sites More sharing options...
chawezul Posted March 18, 2007 Share Posted March 18, 2007 Ah Check your Javascript console to make sure you're not sending: passvar(1, 3, name ,this); Because in that name is a constant! Try: <?php echo "<tr id=\"action\" title=\"Click to display sites below\" onclick=\"passvar(${pageNum}, ${id}, '${name}', this );\">"; ?> thanks! <ins>By the way, in that case the error you would get is: Variable 'name' is undefined in ... on line ...</ins> Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209704 Share on other sites More sharing options...
sayedsohail Posted March 18, 2007 Author Share Posted March 18, 2007 Million thanks, now the alert works, but when i tried to remove alert and redirect the values to a new page it doesn't. here is my modified code. <?php echo "<tr onclick=\"passvar(${pageNum}, ${id}, ${name}, this );\">"; echo "<td>$id</td<td>$name</td></tr>"; ?> <SCRIPT> here is my javascript function. function passvar(pan,rec,nam,obj) { var bage = pan; var cid = rec; var cname = nam; //alert("This is your company name"+cname); document.location.href="sitedetail.php?page="+bage+"&cid="+cid"&cname="+cname; // the problem is here, since i can't retrieve cname value using $name1 = $_GET['cname']; } </SCRIPT> Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209712 Share on other sites More sharing options...
chawezul Posted March 18, 2007 Share Posted March 18, 2007 Does that new page have the values in the query string? You could possibly go <script type="text/javascript"> var cname = <?php echo $_GET["cname"]; ?>; </script> Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209717 Share on other sites More sharing options...
chawezul Posted March 18, 2007 Share Posted March 18, 2007 Err in the new code you also don't have quotations around ${name} Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209719 Share on other sites More sharing options...
sayedsohail Posted March 18, 2007 Author Share Posted March 18, 2007 Me bad, it works fine now, but when i remove the alert and tries to redirects to new page with all the values its giving error, here is my modified code. echo "<tr onclick=\"passvar(${pageNum}, ${id}, '${name}', this );\">"; function passvar(pan,rec,nam,obj) { var bage = pan; var cid = rec; var cname = nam; //alert("This is your company name"+cname); document.location.href="sitedetail.php?page="+bage+"&cid="+cid"&cname="+cname; // the problem lies here now. } Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209725 Share on other sites More sharing options...
sayedsohail Posted March 18, 2007 Author Share Posted March 18, 2007 Thanks chev, everything works now i can sleep well. Quote Link to comment https://forums.phpfreaks.com/topic/43177-solved-echo-problem-passing-values-to-a-function-please-help/#findComment-209733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.