简体   繁体   English

使用ajax将数据插入数据库时​​如何显示警报消息?

[英]How to show an alert message when data is inserted to database using ajax?

I want to show an alert when data is inserted into the database.我想在数据插入数据库时​​显示alert this is the ajax code which sends the request to the EditDeleteLecture.php .这是将请求发送到EditDeleteLecture.php的 ajax 代码。 but the main problem is that when data inserted it shows the alert in Network pane .但主要问题是插入数据时,它会在Network pane显示alert

Ajax Code is that Ajax 代码是这样的

function addRecord() {
    var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form
    $.ajax({
        url: "ajax/EditDeleteLecture.php",
        type: "post",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(output) {
            readRecords();
            $('#form1').trigger("reset");
        }
    });
}

And this is the EditDeleteLecture.php page which inserts the data into the database.这是将数据插入数据库的EditDeleteLecture.php页面。

if (isset($_FILES['files']['name'])) {

    $files = $_FILES['files']['name'];
    $desc = $_POST['description'];
    $subject = $_POST['subject'];
    $path = 'Lectures/'.$files;
    move_uploaded_file($_FILES["files"]["tmp_name"], $path);
    $date = date('d-M-y');

    $query = "INSERT INTO content(file_name,course_code,description,file_path,upload_date) VALUES ('$files','$subject','$desc','$path','$date')";

    $cm = sqlsrv_query($conn, $query);
    if ($cm) {
        echo '<script>alert("data Inserted Successfully");</script>';
    }

}

enter image description here在此处输入图片说明

write your alert in ajax success response like在 ajax 成功响应中编写您的警报,例如

function addRecord() {
    var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form
    $.ajax({
        url: "ajax/EditDeleteLecture.php",
        type: "post",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(output) {
            alert("record inserted successfully.")
            alert(output);
            readRecords();
            $('#form1').trigger("reset");
        }
    });
} 

Alert on ajax response关于 ajax 响应的警报

Update your php code with below code使用以下代码更新您的 php 代码

if (isset($_FILES['files']['name'])) {
   $files = $_FILES['files']['name'];
   $desc = $_POST['description'];
   $subject = $_POST['subject'];
   $path = 'Lectures/'.$files;
   move_uploaded_file($_FILES["files"]["tmp_name"], $path);
   $date = date('d-M-y');
   $query = "INSERT INTO content(file_name,course_code,description,file_path,upload_date) VALUES ('$files','$subject','$desc','$path','$date')";
   $cm = sqlsrv_query($conn, $query);
   if ($cm) {
      $result['status']="Succsess";
      $result['message']="Record Inserted Successfully";
   }else{
      $result['status']="failed";
      $result['message']="Somethink Went Wrong";
   }
 echo json_encode($result);
}

Check status in your ajax code检查您的 ajax 代码中的状态

function addRecord() {
  var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form
  $.ajax({
    url: "ajax/EditDeleteLecture.php",
    type: "post",
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function(output) {
        var json = $.parseJSON(output);
        if(json.status == "Succsess"){
           readRecords();
           $('#form1').trigger("reset");
           alert(json.message);
        }else{
          alert(json.message);
        }
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Ajax代码时获得的数据插入到数据库,显示弹出消息 - Using Ajax code to display a popup message when data get inserted in to the database 如果在MySQL表中找不到数据,如何清除文本框并使用Ajax发送警报消息? - How to clear textboxes and send alert message using Ajax if the data can't be found in MySQL table? 在Ajax警报中显示Rails错误消息 - Show Rails error message in Ajax alert 使用jQuery输入太短时显示警报消息 - Show Alert Message When Input Is Too Short using jQuery 如何使用javascript / jquery在30分钟内没有使用表单字段时显示警告消息? - How to show an alert message when no form field is used in 30 minutes using javascript/jquery? 数据库更新时发出警报消息 - alert message when database updated 当 JWT 令牌在 Angular 7 中过期时如何显示警报或烤面包机消息 - How to show an alert or toaster message when the JWT token expires in Angular 7 如何在React Native中无效的登录凭据时显示警告消息? - How to show alert message when invalid login credentials in React Native? 双击td时如何在警报框中将数据库中的数据显示为html表 - How to show the data from database in a alert box as html table when td is double clicked 如何警告有关文件上传(使用Ajax)完成的消息? - How to alert message about file upload(using ajax) completition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM