Friday, March 19, 2010

[AJAX] form送出資料

jQuery可以用Ajax/serialize 把form的element都轉成query string再用Ajax
或是jQuery form plugin http://www.malsup.com/jquery/form/

Prototype 直接對form用 request就可以了
參考
    $('submit').observe('click', function(){
        $("info-form").request( {onSuccess: function(r) {
            switch(r.responseJSON.error) {
                case 0:
                    alert(r.responseJSON.result);
                    break;
                }
            } } );
        } );


我是覺得用jQuery比較方便
Prototype需要在php令外送header才能接受json

用jQuery做AJAX post的範例 (補填個人資料)
        jQuery("#send", right_block).click( function() {
            jQuery.post('action_url',jQuery("#form_fill_info", right_block).serialize(),
                function(data){
                    alert(data.msg);
                    if (data.error == 0)
                        jQuery("#fill_info", right_block).css('display', 'none');
                },'json'
            );
        });

No comments: