简体   繁体   English

从php访问javascript数组

[英]Access javascript array from php

I want to use javascript array in php.I tried this using JSON, AJAX...And i want to get that array in the same file.but i couldn't find. 我想在php中使用javascript数组。我尝试使用JSON,AJAX进行此操作...而且我想在同一文件中获取该数组。但是我找不到。 can anyone help? 有人可以帮忙吗? ex.php :- 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']);
   ?>
<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']);
   ?>

Try this. 尝试这个。

Don't always echo your HTML code. 不要总是回显您的HTML代码。

Use done: function and not success: function 使用完成:功能而不是成功:功能

If you want to POST it to the same file, leave URL empty 如果要将其发布到同一文件,请将URL留空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM