Tôi đang gặp sự cố khi sử dụng jQuery ajax của mình để hoạt động bình thường. Nó hướng đến trang PHP để cập nhật cơ sở dữ liệu, nhưng không bao giờ quay trở lại kịch bản cho các tùy chọn thành công hoặc lỗi.Lỗi thành công và lỗi chức năng Ajax
Mã của tôi là dưới đây:
$(document).ready(function(){
$("form#updatejob").submit(function() {
function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");}
// we want to store the values from the form input box, then send via ajax below
var job = $("#job").attr("value");
var description = $("#description").val();
description.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
var startDate = $("#startDate").attr("value");
var releaseDate = $("#releaseDate").attr("value");
var status = $("#status").attr("value");
$.ajax({
beforeSend:textreplace(description),
type: "POST",
url: "updatedjob.php",
data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status,
success: function(){
$("form#updatejob").hide(function(){$("div.success").fadeIn();});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
return false;
});
});
Và PHP:
<?php
include("connect.php");
$job = trim($_POST['job']);
$startDate = trim($_POST['startDate']);
$releaseDate = trim($_POST['releaseDate']);
$mysqlstartdate = date('Y-m-d', strtotime($startDate));
$mysqlreleasedate = date('Y-m-d', strtotime($releaseDate));
$description = trim($_POST['description']);
$status = trim($_POST['status']);
$update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' ";
$rsUpdate = mysql_query($update);
// or die(mysql_error()); mysql_close();
?>
Điều gì sẽ xảy ra nếu bạn đặt 'alert()' trong dòng đầu tiên của hàm gọi lại 'thành công'? 'success: function() {alert ('foobar'); ...' – Jasper
Có vẻ logic bạn cũng cung cấp mã php. Bạn đang lặp lại một phản ứng? –
Đây là PHP: ' Php bao gồm (" kết nối.php "); $ job = trim ($ _ POST ['job']); $ startDate = trim ($ _ POST ['startDate']); $ releaseDate = trim ($ _ POST ['releaseDate']); $ mysqlstartdate = date ('Ym-d', strtotime ($ startDate)); $ mysqlreleasedate = date ('Ym-d', strtotime ($ releaseDate)); $ description = trim ($ _ POST ['mô tả']); $ status = trim ($ _ POST ['status']); $ update = "CẬP NHẬT công việc SET startDate = '$ mysqlstartdate', releaseDate = '$ mysqlreleasedate', description = '$ description', status = '$ status 'WHERE jobID =' $ job '"; $ rsUpdate = mysql_query ($ update); // hoặc die (mysql_error()); mysql_close(); ?>' – michael