Blogger Information
Blog 22
fans 0
comment 0
visits 26860
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
$.ajax异步请求的同时要上传用户输入信息的解决方法
kiimi的博客
Original
677 people have browsed it

在作类似百度贴吧的回复功能时,由于要用到ajax异步请求,但是在异步请求的同时需要将用户输入的内容提交到后台并保存到数据库,这时就需要ajax异步提交表单。为了解决这个问题,在网上找了一些资料,最终得到了比较好的解决方法。

方法:在ajax异步请求之前,先将表单数据序列化。然后进行异步请求,这时候后台就可以获取form表单的数据了。

代码如下:

HTML代码:

<body>
    <div class="lz">
        <p style="float: left;color: red;font-size: 20px">楼主</p>
        <div class="name">{$arr[0]['name']}</div>
        <div class="cont">{$arr[0]['cont']}</div>
        <a href="javascript:void(0)" class="hf" title="{$arr[0]['id']}">回复</a>
        <a href="javascript:void(0)" class="hf1">收起回复</a>
    </div>
    <div class="re">
        <form action="" method="post">
            <textarea name="huifu"></textarea>
            <input type="hidden" value="{$arr[0]['id']}" name="lz">
            <label><a href="javascript:void(0)" class="fabiao">发表</a></label>
        </form>
    </div>
    <div class="hflz">
        <volist name="arr1" id="vo">
            <div class="lz">
                <div class="name">{$vo.name}</div>
                <div class="cont">{$vo.cont}</div>
                <a href="javascript:void(0)" class="hf" title="{$vo.id}">回复</a>
                <a href="javascript:void(0)" class="hf1">收起回复</a>
            </div>
            <div class="re">
                <form action="" method="post">
                    <textarea name="huifu"></textarea>
                    <input type="hidden" value="{$arr[0]['id']}" name="lz">
                    <label><a href="javascript:void(0)" class="fabiao">发表</a></label>
                </form>
            </div>
        </volist>
    </div>     <!--回复后显示回复内容的容器-->
</body>

JS代码:

//点击发表进行ajax请求
$('.fabiao').each(function (i,v) {
    $(this).click(function () {
        var form = $('form').eq(i).serialize();//获取需要上传数据的表单,并序列化
        $.ajax({
            type: 'post',
            url: 're',
            data: form,//此处可以提交表单数据
            success: function (data) {
                    var html = '<div class="lz"><div class="name">'+data[0].name+'</div><div class="cont">'+data[0].cont+'</div><a href="javascript:void(0)" class="hf" title="{$arr[0][\'id\']}">回复</a><a href="javascript:void(0)" class="hf1">收起回复</a></div>';
                    $('.hflz').append(html);
            }
        })
    })
})

PHP后台代码:

public function re(){
    $cont = I('post.huifu');
    $lzid = I('post.lz');
    $m = M('zlz');
    $m->lzid = $lzid;
    $m->cont = $cont;
    $m->add();
    $return = $m->order('id desc')->select();
    $this->ajaxReturn($return);
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post