0

I need to, for example, send mail if in this window, a user pressed OK. Can I do it without Ajax?

    var runPHP = confirm("Run PHP script?");

    alert( runPHP );
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • 1
    essentially the answer is NO. PHP runs on the server BEFORE the browser loads the HTML for you to see, Javascript runs in the browser. You need to send a request (http - via ajax ) to the php server to run whatever PHP script you want to call – Professor Abronsius Jun 05 '20 at 13:01
  • ok. thanks a lot, ill try) –  Jun 05 '20 at 13:02

1 Answers1

0

One way to do this without using AJAX would be to create a cookie with JavaScript then use JavaScript again to refresh the page and finally just let PHP use the cookie data that was set earlier to decide whether to run the function or not like this:

JavaScript:

var runPHP = confirm("Run PHP script?");

if(runPHP){
    document.cookie = "runPHP=yes";
    window.location.reload();
} else {
    document.cookie = "runPHP=no";
    window.location.reload();
}

PHP:

<?php

  if(isset($_COOKIE["runPHP"])) {
    if(strpos($_COOKIE["runPHP"],"yes")>=0){
        somePHPFunction();
    }
  }

?>

Logic via - How do I edit PHP variable with JavaScript/jQuery?

AndrewL64
  • 15,794
  • 8
  • 47
  • 79