-4

I run the PHP code by ajax method with the click of a button.

$(".btn_ranking").one('click', function(e) {
    e.preventDefault();
    var name = localStorage.getItem('name');
    var time = localStorage.getItem('timer_end');

    $.ajax({
        url: "php/file.php",
        method: "POST",
        data: { 
            name: name, 
            time: time 
        }
    });
});

I would like the file.php to be able to run the js code, for example:

if ($time < $_SESSION['time']) {
    [...]
}
else {
    echo '<script>alert("lol");</script>';
}

And that when the button .btn_ranking on the page is pressed, an 'lol' alert will be displayed. If it is possible?

Enzo B.
  • 2,341
  • 1
  • 11
  • 33
Adrian12
  • 33
  • 4
  • 1
    Why do not you just return an answer to ajax method ? Something like flag with true/false and then show up alert ? – MorganFreeFarm Jul 19 '18 at 07:41
  • because I don't know, how Can I do to. – Adrian12 Jul 19 '18 at 07:42
  • It is possible, but you have to add the response to the document on the ajax method's success function. OR you can return the data as a json and can do the rest of the javascript execution on the success function – Rakesh Jul 19 '18 at 07:43

3 Answers3

0

you can echo a response to the AJAX call and then run the JS according to the response..

$(".btn_ranking").one('click', function(e) {
        e.preventDefault();
        var name = localStorage.getItem('name');
        var time = localStorage.getItem('timer_end');

        $.ajax({
            url: "php/file.php",
            method: "POST",
            data: { name: name, time: time },
            success: function (data) {
               if(data==1){
               //do this
               }else if(data==2){
               //do that
               alert('LOOL');
               }
            }
        });
    });

PHP CODE:

if ($time < $_SESSION['time']) {
  echo '1';
}
else {
  echo '2';
}
Me1o
  • 1,629
  • 1
  • 6
  • 8
  • in the js code above? its the response data, in this case the data that was outputted by your php code. – Me1o Jul 19 '18 at 08:15
0

You can't said to a server-side script to use javascript.

What you have to do is to handle the return of you'r ajax and ask to you'r front-side script to alert it. Something like that :

file.php :

if ($time < $_SESSION['time']) {
    [...]
}
else {
    echo 'lol';
    exit();
}

Front-side :

$(".btn_ranking").one('click', function(e) {
    e.preventDefault();
    var name = localStorage.getItem('name');
    var time = localStorage.getItem('timer_end');

    $.ajax({
        url: "php/file.php",
        method: "POST",
        data: { 
            name: name, 
            time: time 
        },
        success : function(data) {
            alert(data);
        }
    });
});

When you used ajax for call php script, everything will be print in the return of the php code will be return to the HTTP repsonse and so be on the Ajax return function as params.

Enzo B.
  • 2,341
  • 1
  • 11
  • 33
0

Ok .. First change your js code to handle answer from php script:

$(".btn_ranking").one('click', function(e) {
        e.preventDefault();
        var name = localStorage.getItem('name');
        var time = localStorage.getItem('timer_end');

        $.ajax({
            url: "php/file.php",
            method: "POST",
            data: { name: name, time: time }
            success: function(data) {
              console.log(data);
              // check if it is true/false, show up alert
            }
        });
    });

Then change php script (file.php), something like that:

                $response = [];
                if ($time < $_SESSION['time']) {
                    $response['data'] = false;
                }
                else {
                    $response['data'] = true;
                }

return json_encode($response);

Something like that is the idea :) When u send ajax with POST method get variables from there, not from $_SESSION :)

U can see good example here

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • 1
    https://stackoverflow.com/questions/1506527/how-do-i-use-the-ternary-operator-in-php-as-a-shorthand-for-if-else – mplungjan Jul 19 '18 at 07:49
  • I just write this answer here, not in editor or IDE :) it probably does not work but i want to show the idea :) The ternary operator is always a good idea. – MorganFreeFarm Jul 19 '18 at 07:51
  • @MorganFreeFarm, how can I check if it is true/false? `if (data == false)` or `else if (data == true)`? – Adrian12 Jul 19 '18 at 08:03
  • just open your browser console and see response, that's why i type "console.log(data), you can see response and handle it :) – MorganFreeFarm Jul 19 '18 at 08:05