3

I am trying to make a confirm box which will desire which php code will be executed. Heres my code:

<script type="text/javascript">
    var answer = confirm('Are you sure?');

    if(answer==true)
       {
          <?php $confirmation = 1; ?>
       } 
    else 
       { 
          <?php define("CONFIRMATION", 1, true); ?>
       }
         alert('<?php echo $confirmation; ?>')
         alert('<?php echo defined("CONFIRMATION"); ?>')
</script>

The problem is , even if i click YES, $confirmation and boolean from defined() function returns 1. Whatever I click, (cancel or ok) one of them should be 0 (I've already declared $confirmation before) But both of codes at if and else blocks are used! Normally it works like this

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
sarkolata
  • 370
  • 2
  • 7
  • 17

6 Answers6

14

You fundamentally misunderstand what PHP is doing.

PHP is evaluated on the server before the page is sent to your browser. By the time the browser sees it and executes the javascript, all the PHP is gone.

Use your browser's "view source" on the browser window with this code in it. You'll see it looks like this:

<script type="text/javascript">
var answer = confirm('Are you sure?');

if(answer==true)
   {
             } 
else 
   { 
             }
     alert('1')
     alert('1')
</script>

You either need to implement what you want to do in javascript to run on the browser, or you need to send a new request to the server and get a new page back (either directly or indirectly) with your response.

Francis Avila
  • 31,233
  • 6
  • 58
  • 96
10

That will never work because PHP is processed before the output is sent to the browser. If you really need to modify something in PHP then try using an AJAX call.

http://ajaxpatterns.org/XMLHttpRequest_Call

Or try using jQuery's $.ajax(); function. Start by looking here.

Here is a quick example:

<script type="text/javascript">
var answer = confirm('Are you sure?');
$.ajax({
    type: 'GET',
    url: '/path/to/script.php',
    data: 'answer=' + answer,
    success: function(response) {
        alert(response);
    }
});
</script>

Contents of script.php:

<?php
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) 
        && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
    ) {
        // AJAX request
        $answer = $_GET['answer'];
        // ...
    }
Yes Barry
  • 9,514
  • 5
  • 50
  • 69
4

You can't trigger a PHP code exection without a post/get request. For your needs, you should choose between a form submisssion or load a page-link with parameters stuffed in the query string on confirmation.

P.S. the query string parameters are the ones following the "?" in the format variable=value

for example:

index.php?answered=1

you will be then able to retrieve these vatiable/values using PHP $_POST, $_GET or $_REQUEST variables in a way like this:

if ($_REQUEST['answered'] == 1) { //confirmed
...
}
danicotra
  • 1,333
  • 2
  • 15
  • 34
3

You are misunderstanding the order of what will happen here.

Firstly, PHP will output the javascript layer. Your if block will then look like this:

if (answer == true)
{
}
else
{
}

The javascript engine should then optimise that out and totally ignore it. Consider using AJAX if you need to get PHP to process something with an input from the javascript layer.

Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72
3

Normally it works like this

No, it never works like this. PHP is executed before the javascript so it will never work like this.

I think from what I see you would want something like

<a href="?confirmation=1" onclick="return confirm('Are you sure?')">Your link</a>

This will go to the current page with $_GET['confirmation'] set to "1".

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
2

php executed before javascript so you can't do this because when you check it via javascript if else statement php is already executed so you can't do it but however you can use ajax for it

Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52