-2

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

Bondjens
  • 55
  • 7
  • 2
    There is a huge difference between Java and JavaScript and they also have nothing in common. I think you are referring to JavaScript. – Itay Grudev Feb 27 '14 at 08:16
  • Not really I ask for a solution not why it doesn't do it , I want help with it , not a explenation – Bondjens Feb 27 '14 at 08:17
  • The tag say "javascript" not java, so i guess there was not enough place in the title... – Roadirsh Feb 27 '14 at 08:17
  • No I was thinking about it and accidently put java in it instead of javascript , I use it both and sometimes I mix it with eachother , but I changed it now – Bondjens Feb 27 '14 at 08:19
  • 1
    The explanation you don't want illustrates how things are separated, and point you in the direction of how to cross that divide. The tiny bit of code you show hardly lends itself to an answer containing a fully coded solution. However, I have pointed you towards the door of the solution, you just need to go through it yourself. – deceze Feb 27 '14 at 08:20

3 Answers3

1

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
    });
}
jcubic
  • 51,975
  • 42
  • 183
  • 323
  • How can I do it then? with Ajax because I am totally unfamiliar with Ajax , because Procceed needs to be "Yes" to advance , else it wont , so how can I make sure Procceed gets the value "Yes" within Ajax? – Bondjens Feb 27 '14 at 08:25
  • @Bondjens I've just updated the answer with code that use jQuery (ajax with jQuery library is much simpler then bare JavaScript). – jcubic Feb 27 '14 at 08:27
  • yes I saw it just after I posted that , but Proceed now get's the value of result? And "Script.php" needs to be adjusted to my php page? like this : function myFunction2() { var result = confirm("Are you sure you want to proceed?!"); $.post('Betaling.php', {Proceed: result}, function(result) { // do something if script.php echo something }); } + at this point it doesn't even use the confirm , it just does it , without asking for the confirm – Bondjens Feb 27 '14 at 08:30
  • If you put the same script in ajax call you get the same page in response, better is to just use different script or add check if request is ajax, I often use `''` as url and it send to the same script and in php I do: `if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest') {` – jcubic Feb 27 '14 at 08:37
  • I don't follow now anymore , hhahahah , but what do I need to do with the JQuery , do I need to put something extra with it? To get Procceed = "Yes" ?? – Bondjens Feb 27 '14 at 08:44
  • @Bondjens `{Proceed: result ? 'Yes' : 'No'}` – jcubic Feb 27 '14 at 08:49
0

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

Community
  • 1
  • 1
Martin Baumgartner
  • 3,036
  • 2
  • 17
  • 29
0

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'];
   }
?>
ReeCube
  • 2,215
  • 13
  • 23
  • So I need to put "yourphpurl.php?procceed={YES_OR_NO}" in my php? But the thing is I need to be on the same page , but with Procceed = Yes to go on with the rest of my code – Bondjens Feb 27 '14 at 08:40
  • yes, replace "yourphpurl" by the url of your php you want to execute the procceed command – ReeCube Feb 27 '14 at 08:42
  • Oh ok , and where do I need to put "yourphpurl.php?procceed={YES_OR_NO}" , before my php? Or at the place where my javascript confirm is at this moment? – Bondjens Feb 27 '14 at 08:45
  • at the moment where the user answers your confirmation dialog (in the JavaScript) – ReeCube Feb 27 '14 at 08:54