从php访问javascript数组

时间:2014-10-19 12:38:58

标签: javascript php ajax

我想在php中使用javascript数组。我尝试使用JSON,AJAX ......我想在同一个文件中获取该数组。但我找不到。有人可以帮忙吗?   ex.php: -

<?php
  echo '<html><head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
        </script>
        <script>

        var arr = ["a", "b", "c"];
        var str = JSON.stringify(arr);


            $.ajax({
              type:"POST",
              url: "ex.php",
              data:"jsonstr=" + str,
              dataType:"json",
              success: function(){
                  alert("Test results submitted!");
              }
            });';

         echo '</script></head><body></body></html>';
      ?>

   <?php
       echo json_encode($_POST['jsonstr']);
   ?>

1 个答案:

答案 0 :(得分:0)

<html><head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
        </script>
        <script>

        var arr = ["a", "b", "c"];
        var str = JSON.stringify(arr);


            $.ajax({
              type:"POST",
              url: "",
              data:"jsonstr=" + str,
              dataType:"json",
              done: function(response){
                  alert("Test results submitted!");
              }
            });
</script></head></html>

   <?php
       echo json_encode($_POST['jsonstr']);
   ?>

试试这个。

不要总是回显您的HTML代码。

使用完成:功能而非成功:功能

如果要将其发布到同一文件,请将URL保留为空