ajax提交表单

上传表单的按钮如果是button,需要把类型定为button,否则form表单就不会通过ajax程序传输数据,

因为它的默认类型是submit,

ajax代码:表单中每条数据的name值要和数据表中的字段名一致

$(function(){

    $('#f_add').click(function(){

        var url = "{:U('Fink/add')}";

        var formObj = $('form');

        var data = formObj.serialize();

        $.post(url,data,function(r){

            if(r.status == 200){

                alert(r.msg);

                window.location.href = "{:U('Fink/index')}";

                }else{

               alert(r.msg);

             }

        },'json');

    });

});


ajax提交表单_第1张图片

后台代码:

function add(){

if(IS_POST){

$data = I('post.');

// dump($info);die;

$fink = M('Fink');

if($fink->add($data)){

$data['status'] = 200;

$data['msg'] = '添加成功';

$this->ajaxReturn($data);

}else{

$data['status'] = 400;

$data['msg'] = '添加失败';

$this->ajaxReturn($data);

}

}else{

$this->display();

}

}


ajax提交表单_第2张图片

你可能感兴趣的:(ajax提交表单)