-1

So while i load the page there is a javascript pop up box with two button ( confirmation box ) If user click on OK i have to run a php code if user clicks on cancel i have to run another php.

I taught thats the easiest way to do this is that if i create a variable in php and i modify it in js, and after this i just do an if/else method:

<?php
....
$atir=-1
    print '<script type="text/javascript">
                var retVal = confirm("Quetion?")
                if(retVal==true){
                    $atir=1
                                }
                else {
                    $atir=0
                        }
        </script>';

      if($atir==1){}

      else if($atir==0){}
...?>

I've read that ajax is can be do sg like this, but ajax is after that the page loaded, and i want to do this while the page is loading, and thats while I think this is possible.

Daniel
  • 33
  • 1
  • 8
  • PHP is a server side language , and it will not wait for javascript to answer the confirmation , it will just proceed further with it's code , as javascript in your situation will react as just normal text inside it. nothing else. You can load the page and then after page is ready , you can call the confirmation code and ask client about what he want , then when the confirmation happens , call the server with ajax and load it's content. – Arsh Singh Aug 01 '16 at 10:25
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Epodax Aug 01 '16 at 10:26

1 Answers1

0

You can achieve this from simply JS.

<script type="text/javascript">
    var retVal = confirm("Question?")

    if(retVal == true) {
        // path to php file if condition is true
        window.location='first_file.php';
    } else {
        // path to php file if condition is false
        window.location='second_file.php';
    }
</script>
Deepak Adhikari
  • 419
  • 2
  • 4