【问题标题】:How to activate PHP file in JavaScript function如何在 JavaScript 函数中激活 PHP 文件
【发布时间】:2011-04-03 04:23:23
【问题描述】:

我正在尝试在激活 javascript 函数时将一些信息写入我的数据库。

我使用 PHP 和 MySQL。如何打开 .php 文件,执行它并返回 .js 文件以使函数继续运行?

提前致谢。

【问题讨论】:

    标签: php javascript database


    【解决方案1】:

    首先,不能直接从JavaScript调用PHP函数,反之亦然。这是因为 PHP 是服务器端脚本,运行在服务器上,而 JavaScript 是客户端脚本,运行在浏览器上。

    但是,有一个解决方案,使用一种称为“AJAX”的技术(A同步 JavaScript and XML),可用于从 JavaScript 向服务器发送请求。

    例如,使用用户看到的“用户”页面和从 JavaScript 代码调用的“请求”页面,我可以编写以下代码:

    userpage.php

    <!-- JavaScript code -->
    <script type="text/javascript">
    function sendRequestToServer()
    {
      // The XMLHttpRequest object is used to make AJAX requests
      var ajax = new XMLHttpRequest();
      // The onreadystatechange function will be called when the request state changes
      ajax.onreadystatechange = function()
      {
        // If ajax.readyState is 4, then the connection was successful
        // If ajax.status (the HTTP return code) is 200, the request was successful
        if(ajax.readyState == 4 && ajax.status == 200)
        {
          // Use ajax.responseText to get the raw response from the server
          alert(ajax.responeText);
        }
      }
      // Open the connection with the open() method
      // (the third parameter is for "asynchronous" requests, meaning that
      //   JavaScript won't pause while the request is processing).
      ajax.open('get', 'requestpage.php', true);
      // Send the request using the send() method
      ajax.send();
    }
    </script>
    <!-- HTML code -->
    <button onclick="sendRequestToServer();">Send request!</button>
    

    requestpage.php(此页面的输出将返回到您的 JavaScript 代码):

    <?php
    echo "Hello World!";
    ?>
    

    此示例将在按下按钮时向服务器发送 HTTP 请求,请求 requestpage.php,服务器将执行一些服务器端代码并​​回显结果。然后,浏览器将从服务器接收到的数据并在脚本中使用它——在本例中为alert()

    一些资源:

    您可能还想查看 JSON 编码,这是在客户端和服务器之间发送对象和数组的非常常见的方法(尤其是在使用 AJAX 时):

    (抱歉这么长的回答,希望对您有所帮助)

    【讨论】:

      【解决方案2】:

      如果您还没有使用支持 AJAX 的框架(例如 jQuery),您可以使用真正轻量级的 XHR 实现来发出 HTTP 请求。此请求可以将任何 PHP 资源(执行所需的数据库更新)作为目标。

      我知道的最小代码在这里:http://dengodekode.dk/artikler/ajax/xmlhttprequest_wrapper.php(丹麦语,抱歉)

      <script type="text/JavaScript">(function(){if(window.XMLHttpRequest)return;var o=null,s,
      a=["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
      for(var i=0,j=a.length;i<j;s=a[i],i++){try{if(o=new ActiveXObject(s))break}
      catch(e){}}window.XMLHttpRequest=o?function(){return new ActiveXObject(s)}:null;o=null})()</script>
      

      请求:

      var oHttp = new XMLHttpRequest();
      oHttp.open("post", "http://www.domain.dk/page.php", true);
      oHttp.onreadystatechange = function(){ myCallBack(oHttp) };
      oHttp.send("id=123&noget=andet");
      

      【讨论】:

        【解决方案3】:

        我想你可能有点困惑。 Javascript 在客户端计算机上的浏览器中运行。 Php/MySQL 在服务器上运行,响应 HTTP 请求,并创建内容供浏览器显示/运行。

        为了让两者动态通信,您需要查看如何从客户端的 javascript 向服务器上的 php 脚本发送/接收 HTTP 请求。您还需要能够在 javascript 中处理响应。这种做法称为 AJAX。根据我的经验,最简单的方法是使用 JSON 和 jQuery,http://api.jquery.com/jQuery.getJSON/

        【讨论】:

          【解决方案4】:

          查找 AJAX... 也可以考虑使用 jQuery 它有一个简单易用的 ajax() 函数。

          【讨论】:

            【解决方案5】:

            您将需要 AJAX,http://www.ajaxf1.com/tutorial/ajax-php.html 有一个使用 PHP 服务器的 AJAX 简单教程

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-01-03
              • 2017-06-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-11-02
              • 1970-01-01
              相关资源
              最近更新 更多