简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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