0

I have this code:

<html>
<body>
<script>
    var x;
if (confirm("Press a button!") == true) {
    x = "You pressed OK!";
  <?php $kk="ok"; ?>
} else {
    x = "You pressed Cancel!";
   <?php $kk="not ok"; ?>
}
document.getElementById("demo").innerHTML = x;
</script>
<?php 
echo $kk;
?>
</body>
</html>

When I echo $kk, I obtain always not ok

But I want to print OK or NOT OK. Any helps please?

A J
  • 3,970
  • 14
  • 38
  • 53
mongoDB
  • 23
  • 5

1 Answers1

0

What you are asking for is absolutely impossible as is, because all PHP code is completely executed before the html is sent to the browser, where the javascript is executed in its turn, hence after.

If you need the result on the http server, you must describe a form in the html page and create a server PHP script at the URL of the form action, then the PHP can get the form elements.

If you need the result in the browser (eg. to change some elements in the page displayed), you must code in javascript.

If you want to exchange between PHP and javascript while staying in the same page, you can use Ajax, or you can use directly the javascript XMLHttpRequest object which can call a server script from the browser javascript. See some examples at w3schools.com

J.P. Tosoni
  • 549
  • 5
  • 15