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

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

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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 R语言绘制火山图对数据使用case when函数报错
      • ¥15 关于对一篇论文的统计图作图产生好奇
      • ¥15 如何使用RISC-V对斐波那契递归算法进行优化
      • ¥15 OBS 虚拟摄像头和真实摄像头区别
      • ¥15 yolov5运行detect 过程中的报错一直无法解决
      • ¥15 jsx写法 插槽不生效的问题
      • ¥15 MATLAB 调用comsol如何加速设计?
      • ¥15 echarts地图添加点击事件
      • ¥15 openpyxl库无法全部写入文本到单元格
      • ¥15 不知道带什么标题好随便搞一个吧