douyi6922 2014-08-26 13:59
浏览 86
已采纳

如何在插入数据成功时重置表单? (阿贾克斯)

I have a form. I am processing the form through ajax

Below php file is called through ajax and it validates data. If there are errors, they are displayed. If not data is inserted into the database. Success result is also displayed

if (!empty($errors)) {
    echo "<div class='alert alert-danger'>"
    foreach($errors as $error) {
        echo "<p>".$error."</p>";
    }
    echo "</div>";

} else {
    //no errors     
    $result = $database - > create("customer", $data);
    if ($result) {
        echo "<div class='alert alert-success'>"
        echo "<p>New customer created successfully</p>";
        echo "</div>";
    }
}

Everything works as I expected, but I want to make an enhancement. I need to clear the form only when success result is displayed.

If there are errors I do not want to reset the form.

I can check whether output string is equal to the whole success message and if yes reset the form, but I am looking for some solution which does not depend on the output string.

$(function () {
    $("#create-form").submit(function (e) {
        e.preventDefault();
        $.ajax({
            method: "POST",
            context: this,
            url: "create_process.php",
            data: $(this).serialize(),
            success: function (data) {
                $("#update").html(data);
                $('#create-form').trigger("reset");
                //How to check whether output data is either
                //an error or a success and call above reset function
            }
        });
    });
});
  • 写回答

3条回答 默认 最新

  • doumu5934 2014-08-26 14:09
    关注

    Return a JSON from your php...

    $res['html'] = "";
    $res['status'] = "";
    if (!empty($errors)) {
        $res['html'] .= "<div class='alert alert-danger'>";
        foreach($errors as $error) {
            $res['html'] .= "<p>".$error."</p>";
        }
        $res['html'] .= "</div>";
        $res['status'] = "error";
    } else {
        //no errors     
        $result = $database - > create("customer", $data);
        if ($result) {
            $res['html'] .= "<div class='alert alert-success'>";
            $res['html'] .= "<p>New customer created successfully</p>";
            $res['html'] .= "</div>";
        }
        $res['status'] = "success";
    }
    
    echo json_encode($res);
    

    In your jquery:

     $.ajax({
        method:"POST",
        context:this,
        dataType: "json",
        url:"create_process.php",
        data:$(this).serialize(),
        success:function(data){                      
            $("#update").html(data.html);           
            if (data.status == "success") {
                $('#create-form').trigger("reset");
            }
    
        }                  
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮