[英]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>';
}
}
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.