简体   繁体   中英

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. this is the ajax code which sends the request to the EditDeleteLecture.php . but the main problem is that when data inserted it shows the alert in Network pane .

Ajax Code is that

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.

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

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

Update your php code with below code

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

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);
        }
    }
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM