Tom Cruise Tom Cruise - 3 months ago 11
PHP Question

Post array to PHP through ajax

I'm trying to post an array through ajax to php file on form submit.

<form action="echo.php" method="post">
<input name="qwerty[]" type="text">
<input name="qwerty[]" type="text">
<input type="submit"/>
</form>


Basically, I use this to post to php file:

function getlist(alt) {
$.ajax({
type:'POST',
url: 'markattlist.php',
data:{today:alt},
success: function(data){
$('#helloflist').html(data);
},
error:function (){}
});
}


Above is an example of what I'm trying to do. I've searched, but unable to find the solution to that. How can I post an array through ajax.

Answer

Instead of bothering how to send what you need, I advise you to use jquery serialize() method:

function getlist(alt) {
    $.ajax({
    type:'POST',
    url: 'markattlist.php',
    data: $("form").serialize(),
    success: function(data){
        $('#helloflist').html(data);
    },
    error:function (){}
    });
}

And check on server-side with

print_r($_POST);
Comments