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 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表