Takazudo hamalog

programming notes. mainly about JavaScript / jQuery. [@Takazudo] [takazudo@gmail.com] Hint: alt + /

cool guy

How to post array to php via ajax

2011/09/15 permalink

Simply do jQuery.fn.serialize to the form and post it to PHP.

$(function(){
    var $form = $('#form');
    var $res = $('#res');
    $form.submit(function(e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'receiver.php',
            data: $form.serialize()
        }).done(function(res){
            console.log(res); // got ajax result yey
            $res.empty().html(res);
        });
    });
});
<form action="receiver.php" method="post" id="form">
<input type="text" name="test[0][foo]" value="0-foo-val" />
<input type="text" name="test[0][bar]" value="0-bar-val" />
<input type="text" name="test[0][hoge]" value="0-hoge-val" />
<input type="text" name="test[1][foo]" value="1-foo-val" />
<input type="text" name="test[1][bar]" value="1-bar-val" />
<input type="text" name="test[1][hoge]" value="1-hoge-val" />
<input type="submit" value="POST NOWWWW">
</form>

PHP just parses the key strings of received data and make array from it. (maybe)

blog comments powered by Disqus

  1. hamalogの投稿です