Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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