从JavaScript调用PHP方法 [英] Calling a PHP Method from JavaScript

查看:120
本文介绍了从JavaScript调用PHP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是,由于JavaScript是客户端,PHP是服务器端,因此不可能从JavaScript调用PHP;不过,这段代码片段仍然有效:

I was under the impression that since JavaScript is client-side and PHP is server-side, that it is impossible to call PHP from JavaScript; however, this code snippet works:

<script>
  function otherCourse(){
    var course = prompt("prompt?");
    document.write("
      <?php
        $con->query("INSERT INTO `Courses` (`Name`) VALUES ('blah')");
      ?>
    ");
  }
</script>

为什么这会起作用?该条目被插入到数据库中

Why does this work? The entry was inserted into the database

推荐答案

您的PHP正在从服务器端执行。您嵌入到JavaScript中的PHP永远不会实际呈现任何内容。

Your PHP is being executed from the server side. Your PHP that you've embedded in the JavaScript never actually ends up rendering anything.

通过移除对 otherCourse 函数,你会发现查询仍然运行。

Test it by removing any calls to the otherCourse function, and you'll see that the query still runs.

这篇关于从JavaScript调用PHP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆