-3

I write some html, javascript and php code that normally have to execute php code when a html button is pressed if you click on "ok" on the "confirm box", so there is my html code :

<input onclick="logout()" name="logout" type="submit" value="Logout me" />

Javascript and php code :

function logout() {
  if (confirm("Are you sure you want to logout ?") == true) {
    <?php echo "Hello world !"; ?>
  } else {
    <?php echo "Goodbye world !"; ?>
  }
}

I think that the javascript code work because when i click on the html button the confirm box appear like it has to appear, but the php code is not executed.

Please if someone would help me it will be greatfull.

Christoph
  • 50,121
  • 21
  • 99
  • 128
jestkoks
  • 17
  • 2
  • 9
  • 1
    You have to provide a better example of what you want. A literal string is the same without PHP code. Is there some logic to execute? If yes, you can't do by that way. – Gabriel Heming May 02 '17 at 19:29
  • 7
    You need to understand the difference between _server-side_ code and _client-side_ code. You need either JS or AJAX. – SLaks May 02 '17 at 19:29
  • To explain why it does not work, PHP code is executed server-side, so when it comes to the browser, it will be : `Hello world !` instead of `` which will probably trigger an error as the javascript won't be valid. – Coldiary May 02 '17 at 19:32

3 Answers3

4

You can't use php in javascript because php is a serverside programming lenguage and javascript is a client side programming lenguage, so php is executed (by the server) before javascript (that is executed by the browser of the user). You have to use Ajax requests for your needs.

Quickstart Ajax guide: https://www.w3schools.com/xml/ajax_intro.asp

Helio
  • 193
  • 2
  • 11
1

Even though it is possible to write JS inside of PHP, I don't think this is the way to solve this problem. You can only (as far as I know) echo PHP values inside JS that is executed inside tags, when you write HTML inside .php files.

PHP is indeed server side and the Javascript is in this situation client side. You're thinking wrong.

First you should extract your logout functionality to a logout.php file for example. Then you could do either one of the two:

  1. When you really want to use the logout function as a direct event listener to the onClick event on your input, your logout callback function should make an AJAX call to logout.php, which logs out your user. See the answer from @Helio to see more about AJAX.

  2. Your input should be placed inside a <form>, which has an action attribute, that calls to your logout.php. Then when submit is called, it is available to you through the $_POST superglobal, and you can check if it is pressed. Then you simply log out the user.

Fabian Tjoe A On
  • 1,383
  • 5
  • 18
  • 41
1

That doesn't appear to be valid Javascript. Within the Javacript function, you are dropping a PHP statement as if you were writing it to the HTML, this will not work. I'm surprised you got the dialog to popup. PHP is a server side language and Javascript is not, therefore you should not even call PHP code like that from any kind of Javascript.

To help your curiosity, you could get away with using the document.write() function, but don't use this. I would recommend rewriting your function to redirect to a specific PHP page.

function logout() {
       if (confirm("Are you sure you want to logout ?") == true)
       {
            // try to avoid this....but still works
            document.write('<?php echo "Hello world !"; ?>');

            // try this instead
            window.location.href = '/helloworld.php';
       }       
       else
       {
           // try to avoid this....but still works
            document.write('<?php echo "Goodbye world !"; ?>');

            // try this instead
            window.location.href = '/goodbyeworld.php';
       }
  }