当前位置: 动力学知识库 > 问答 > 编程问答 >

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. can anyone help?

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.

Use done: function and not success: function

If you want to POST it to the same file, leave URL empty

分享给朋友:
您可能感兴趣的文章:
随机阅读: