dongxun6690 2017-05-02 19:27
浏览 99

将php插入javascript

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.

  • 写回答

3条回答 默认 最新

  • dongyu1125 2017-05-02 19:30
    关注

    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

    评论

报告相同问题?