1

I want to post an array to PHP using AJAX and on success returns the value back to JavaScript. Here's my code.

JavaScript:

$(document).ready(function(){
$.ajax({
    type: "POST",
    url: "phparray.php",
    data: {
        array1: phparray
    },

    success: function(data){
        alert("success");
        alert(data);
    }
});
});

HTML:

<html>
<head>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script>
     var phparray = jQuery.makeArray();
     for(var i=0; i<10 ; i++){
        phparray.push(i); 
     }

    </script>
    <script type="text/javascript" src="phparraypost.js"></script>
</head>
<body>
</body>
</html>

PHP:

<?php
$n=$_POST['array1'];

echo $n;


?>

The data I get back says

<br /> <b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\php\phparray.php</b> on line <b>4</b><br /> Array

and I don't have an idea what might be wrong with it.

The HTML, PHP and JavaScript code are in different files.

6
  • 2
    So what isn't working exactly. Other than using jQuery to create an empty array, there doesn't seem to be anything wrong? Commented Jul 27, 2013 at 13:30
  • The 'path' between a client (javascript) and the server (php) is HTTP. Http doens't care about arrays and only accepts strings. In your javascript you should format the array to a string (comma seperated, JSON) and parse it serverside to get the array back. Commented Jul 27, 2013 at 13:30
  • @Joren - jQuery will do that for you. Commented Jul 27, 2013 at 13:31
  • @adeneo that's why I didn't state is as an answer but as a comment. Commented Jul 27, 2013 at 13:32
  • the data i get back says "<br /> <b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\php\phparray.php</b> on line <b>4</b><br /> Array" and i don't have any idea about it. Commented Jul 27, 2013 at 13:37

4 Answers 4

5

The problem is in your PHP - you are using echo on an array. Instead use var_dump.

<?php
    $n=$_POST['array1'];
    var_dump($n);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

how can i use $n in the form of an array in the php file? any suggestion, please!
Well $n should already be an array so you could easily traverse it with foreach etc. Try var_dump($n) and see what kind of data type is reported back (it should be Array).
var_dump($n) returns "array(10) { [0]=> string(1) "0" [1]=> string(1) "1" [2]=> string(1) "2" [3]=> string(1) "3" [4]=> string(1) "4" [5]=> string(1) "5" [6]=> string(1) "6" [7]=> string(1) "7" [8]=> string(1) "8" [9]=> string(1) "9" } " I think I might just get what i wanted.... thank you.
3

I came up with the solution using $.param. This function is used internally to convert form element values into a serialized string representation. I am not sure if it fulfills your requirement.

   <script>
      $(document).ready(function(){
        var phparray = new Object();
         for(var i=0; i<10 ; i++){
            phparray['val' + i] = i; //store value in object
         }

        $.ajax({
            type: "POST",
            url: "phparray.php",
            data: {
                array1: $.param(phparray) //serialize data
            },

            success: function(data){
                alert("success");
                alert(data);
            }
        });
      });
    </script>

PHP

 $pieces = explode('&', $_POST['array1']); //explode passed serialized object
 $phparray = array();
 foreach($pieces as $piece){
   list($key, $value) = explode('=', $piece);
   $phparray[$key] = $value; //make php array
 }

 var_dump($phparray);

Comments

0

http://api.jquery.com/serializeArray/ does this for you, since your already using jquery..

Comments

0

use this:

$("#formId").submit(function(event) {
        event.preventDefault();
        var url = $(this).attr('action');
        $.ajax({
            url : url,
            data : $(this).serialize(),
            cache : false,
            contentType : false,
            processData : false,
            type : 'POST',
            success : function(data) {
                $('#result').html('<div class="notes">Done</div>');
            }
        });
        return false;
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.