0

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']);
   ?>
  • 2
    What exactly are you trying to accomplish here? It looks like you're trying to echo a value from the server before you even post it to the server, and then when you do post it to the server you ignore the resulting value. It looks like you're confused on the difference between server-side and client-side code, but it's difficult to advise without knowing what you're actually trying to do. – David Oct 19 '14 at 12:46

1 Answers1

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']);
   ?>

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

Cr41s3
  • 97
  • 2
  • 11