This question already has an answer here:
Hey there I have a problem , I need to check with a confirm pop-up if somebody wants to procceed or to stay on the page , like this :
function myFunction2()
{
var r=confirm("Are you sure you want to proceed?!");
if (r==true)
{
}
}
I want to have the variable $Procceed="Yes"
within the if and a bit later I want to use the variable to do something
<?php
if(isset($_POST['Procceed']) && $_POST['Procceed'] == 'Yes'){}?>
How can I do this the best??
</div>
You just need to add a function which creates a post-request in your java-script function, a good description could be found here: JavaScript post request like a form submit
You can't because php is run before JavaScript even if in your code it's after. The flow is a follow: the php print your javascript then check the condition and do something. then the user recieve the page and the javascript is run then the confirm will be executed.
If you want do something in php and check $_POST['Proceed']
then you need to use ajax to send the variable to php.
if you can use jQuery it will be something like:
function myFunction2() {
var result = confirm("Are you sure you want to proceed?!");
$.post('script.php', {Proceed: result}, function(result) {
// do something if script.php echo something
});
}
Use jQuery Ajax and give the procceed variable as GET parameter: "yourphpurl.php?procceed={YES_OR_NO}"
in php you can access this variable with:
<?php
if (isset($_GET['Procceed'])) {
$Procceed = $_GET['Procceed'];
}
?>