【问题标题】:communication between PHP and JavascriptPHP和Javascript之间的通信
【发布时间】:2012-12-09 20:28:05
【问题描述】:

我想从 PHP 向 Javascript 发送一些数据。(这两个文件是同一文件夹中的不同文件) 例如,如果我在 PHP 端计算一些值,我想将数据发送到 javascript 并使用数据。 我该怎么做??

【问题讨论】:

    标签: php javascript


    【解决方案1】:

    有一个完整的技术称为AJAX,其中有很多tutorials on the internet

    而且已经有一个很棒且易于部署的实现 - within jQuery

    【讨论】:

      【解决方案2】:

      在实践中,您可以这样使用:

      文件:index.php

      <HTML>
          <body>      
              <input type="text" id="test" value="123"><br>
              <input type="button" id="btn" onclick="send_to_php()" value="send to php">
      
              <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
              <script>
      
                      function send_to_php() {
                              $.ajax({
                                  url: 'js_php.php',
                                  type: 'POST',               
                                  // Form data
                                  data: function(){
                                      var data = new FormData();
                                      data.append('test', $("#test").val() );     
                                      return data;
                                  }(),
                                  success: function (data) {
                                      var obj = JSON.parse(data);
                                      $("#test").val( obj.result );                   
                                  },
                                  error: function (data) {
                                      console.log(data);
                                  },
                                  complete: function () {                 
      
                                  },
                                  cache: false,
                                  contentType: false,
                                  processData: false
                              });
                      }
      
              </script>
          </body>
      </HTML>
      

      文件:js_php.php

      <?php
          //FILE: js_php.php
      
          $test = $_POST["test"];
          $test .= "456";
      
          $arrResult = array(     
              'result' => $test
          );          
      
          print json_encode($arrResult);
          die();
      ?>
      

      文件“index.php”是JavaScript和PHP之间使用jQuery Ajax方法的通信。 当点击“send to php”按钮将运行“send_to_php()”,它将获取输入id“test”的值并通过ajax发送“js_php.php”文件。 反过来,文件“js_php.php”将接收这个变量作为 POST,修改并打印 JSON 格式的值。 ajax 函数“send_to_php()”实现的方法将“监听”所有“js_php.php”打印。

      返回成功后,javascript将文本“js_php.php”打印在一个JSON对象上,然后JS可以在javascript代码内处理:

      success: function (data) {
          var obj = JSON.parse (data);
          $("# test").val(obj.result);
      },
      

      【讨论】:

        【解决方案3】:
        <script type='text/javascript'>
        var myVar = <?php echo $myVar; ?>;
        </script>
        

        简而言之。不过还有更复杂的交流方式。

        【讨论】:

          【解决方案4】:

          看看这个 AJAX 教程:http://news.php.net/php.general/219164

          【讨论】:

            【解决方案5】:

            在 PHP 页面的 script 标签中分配一个 JavaScript 全局变量,并在后面包含其他 javascript 文件。

            示例:

            <html>
              <head>
                <script type='text/javascript'>var testGlobal = <?php echo $globalJSValue ?></script>
                <script type='text/javascript' src="url"></script>
                <script type='text/javascript' src ="url"></script>
              </head>
            </html>
            

            testGlobal 变量现在可用于两个 javascript 文件。

            【讨论】:

              猜你喜欢
              • 2012-11-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-08-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多