Jump to content

"Are you sure" box with PHP variable?


lynsey93

Recommended Posts

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

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..

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.