doujiabing1228 2013-06-24 01:59
浏览 106
已采纳

使用ajax提交后隐藏表单

I've created a form using a bit of jquery, ajax and php. The ajax, validation and php processing works well. I came up with the following code to hide my form after submitting, but I don't know if this is the good way to do that. I would like your advise.

Below the js script with the ajax call

<script type="text/javascript">
$(document).ready(function(){
$("#submitContact").click(function() {
    $.ajax({
    cache: false,
    type: 'POST',
    url: 'form/process.php',
    data: $("#contact_form").serialize(),
    success: function(response) {
        $("#formstatus").hide().html(response).slideToggle(600);
    }
    });
});
});
</script>

The above code will call the php to validate and populates the div#formstatus to notify the user if the form is sent or not.

Within my process.php, I will hide the form if there are no errors found using echo js script.

// If no errors found echo succes message
if (!($formerrors)) :

    //Hide the form
    echo '<script type="text/javascript">
            $(document).ready(function(){
               $("#contact_form").hide();
            });
          </script>';

    // display success message
    echo '<div>
            Your message has been sent. <br />
            Thank you for contacting us!
          </div>';

    Etc....

To sum up:

Should I use this method to hide my form? (Because it does work)

Note. I'm new to jquery and ajax :)

  • 写回答

3条回答 默认 最新

  • dptiq46022 2013-06-24 02:09
    关注

    I think it would be better to return a JSON object which contains the information relevant to the request.

    For example:

    if (!($formerrors)){
        echo json_encode(array(
            'success'=> true,
            'message'=> 'Your message has been sent. <br> Thank you for contacting us!'
        ));
    }
    else{
        echo json_encode(array(
            'success'=> false,
            'message'=> 'Error processing form',
            'errors'=> array(
                            'phone'> 'A phone number is required'
                        )
        ));
    }
    

    This will be easy to work with on the client side (jQuery will automatically parse the json)

    <script type="text/javascript">
    $(document).ready(function(){
    $("#submitContact").click(function() {
        $.ajax({
        cache: false,
        type: 'POST',
        url: 'form/process.php',
        data: $("#contact_form").serialize(),
        success: function(response) {
            if(response.success){
                $("#formstatus").hide().text(response.message).slideToggle(600);
            }
            else{
                jQuery.each(response.errors,function(){ ... });
            }
        }
        });
    });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题