weixin_33671935 2017-10-20 15:58 采纳率: 0%
浏览 446

通过AJAX请求删除

I have a button which call this AJAX request to delete record. How could I show/handle mysql errors correctly like:

Error: Table 'supplier_contacts' doesn't exist

// DELETE
$('.delete-btn').click(function () {

  // Confirm
  if (!confirm('Are you sure want to delete this row?')) {
    return false;
  }

  // id need to delete
  var contact_id = $(this).attr('contact_id');

  // Current button 
  var obj = this;

  // Delete by ajax request
  $.ajax({
    type: "post",
    dataType: "text",
    url: 'suppliers_sql.inc.php?a=delete_contact',
    data: {
      contact_id: contact_id
    },
    success: function (result) {
      $(obj).parent().parent().remove();
      window.location.assign('suppliers_details.php?id=<? echo $supplier_id ?>&m=success');
    }
  });

});


SQL QUERY of suppliers_sql.inc.php ? a = delete_contact

// --------------------------------------------------------------------------------------------
// DELETE CONTACT
// --------------------------------------------------------------------------------------------     
if ($_REQUEST['a'] == "delete_contact") {

  $contact_id = $_POST['contact_id'];

  $sql_contact = "DELETE FROM supplier_contacts WHERE contact_id = $contact_id";

  if (mysqli_query($mysqli, $sql_contact)) {
    mysqli_close($mysqli);
    //header("Location: suppliers_details.php?id=$supplier_id&m=success");
    exit;
  } else {
    echo "Error: " .$sql_contact. "<br>" .mysqli_error($mysqli);
    mysqli_close($mysqli);
    exit;
  }

}
  • 写回答

1条回答 默认 最新

  • weixin_33747129 2017-10-20 16:09
    关注

    As you are echoing something if there is an error you can display it in your success.

       $.ajax({
       type : "post",
       dataType : "text",
       url : 'suppliers_sql.inc.php?a=delete_contact',
       data : {
          contact_id : contact_id
          },
          success : function(result){
             if(result)
             {
                //Your error
                alert(result);
             }
             else
             {
                //all good
                $(obj).parent().parent().remove();
                window.location.assign('suppliers_details.php?id=<? echo $supplier_id ?>&m=success');
             }
          }                       
       });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝