从php访问javascript数组

从php访问javascript数组,javascript,php,ajax,Javascript,Php,Ajax,我想在php中使用javascript数组。我尝试使用JSON、AJAX…并希望在同一个文件中使用该数组。但我找不到。有人能帮忙吗? 例如php:- <?php echo '<html><head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js </script> <script>

我想在php中使用javascript数组。我尝试使用JSON、AJAX…并希望在同一个文件中使用该数组。但我找不到。有人能帮忙吗? 例如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']);
   ?>
试试这个

不要总是重复你的HTML代码

使用done:function和notsuccess:function


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

您在代码中做了哪些更改,为什么它可以解决问题?您在这里到底想实现什么?看起来您试图在将某个值发布到服务器之前从服务器回显该值,然后在将其发布到服务器时忽略结果值。看起来您对服务器端代码和客户端代码之间的区别感到困惑,但是在不知道您实际要做什么的情况下,很难给出建议。
<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']);
   ?>