lynsey93 Posted August 16, 2011 Share Posted August 16, 2011 Hello, I have a "remove" button on some features of my site, with a prompt that asks "are you sure" before continuing on. The problem I am having is that I'm not sure how to get the PHP variable I need into the URL when the user clicks through. The remove button is on each entry in a for loop, so I need it to pass the specific id of the project to be removed. print ' <script type="text/javascript"> <!-- function queryAction2() { var confirmmessage = "Are you sure you want to sign up for '.$name.'? YOU MUST SHOW UP!"; var goifokay = "projsignup.php?id='.$id.'"; var cancelmessage = "Cancelled"; if (confirm(confirmmessage)) { window.location = goifokay; } else { } } //--> </script>'; It always passes the final variable in the for loop, and thus the last project is always removed. Which makes sense, I'm just not sure how to correct it. I don't know much java. Thanks! Link to comment https://forums.phpfreaks.com/topic/244900-are-you-sure-box-with-php-variable/ Share on other sites More sharing options...
AyKay47 Posted August 16, 2011 Share Posted August 16, 2011 from seeing this code alone, I have no idea what is going on.. why are you using PHP variables $name and $id in a javascript function, whithout passing them as arguments...? where is the confirm function coming from..? I will need more information if you would like me to be able to help you here.. Link to comment https://forums.phpfreaks.com/topic/244900-are-you-sure-box-with-php-variable/#findComment-1258067 Share on other sites More sharing options...
lynsey93 Posted August 17, 2011 Author Share Posted August 17, 2011 That's probably the problem. How do I pass PHP variables to javascript? Link to comment https://forums.phpfreaks.com/topic/244900-are-you-sure-box-with-php-variable/#findComment-1258781 Share on other sites More sharing options...
Omirion Posted August 17, 2011 Share Posted August 17, 2011 There are a few methods. I'll start from stupidest (though easiest, and work up from there). Method 1: You echo them. Like so: var jsVar = "<?PHP echo $phpVar?>"; Method 2: (Most effective considering learning curve and results) You create a php script that returns the needed variable. Then call that script with AJAX within your JS code. Tutorial: http://www.w3schools.com/ajax/default.asp Method 3: (only if your project is big) Use this - http://jquery.hohli.com/ In conjunction with jQuery - http://jquery.com/ Bonus: I think the frameworks: Prototype - http://www.prototypejs.org/ And DojoToolkit - http://dojotoolkit.org/ Have easy methods but i never used them so, you're on your own. Good luck. Link to comment https://forums.phpfreaks.com/topic/244900-are-you-sure-box-with-php-variable/#findComment-1258813 Share on other sites More sharing options...
AyKay47 Posted August 18, 2011 Share Posted August 18, 2011 you can also pass php variables as arguments to a javascript function.. eg <head> <script type='text/javascript'> function someFunc(variable){ document.write(variable); } </script> </head> <span onClick="someFunc(<?php echo $variable; ?>)"></span> Link to comment https://forums.phpfreaks.com/topic/244900-are-you-sure-box-with-php-variable/#findComment-1258961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.