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

    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题