我试图使用 ajax 发送一个 php 数组值,但什么也没发生,MySQL 表也没有更新......
.html:
//edit topic
if(isset($_GET['edit_topic']) && is_numeric($_GET['edit_topic']) ){
$topic_id=htmlspecialchars($_GET['edit_topic']);
$get_topic=$db->query("select * from articles where art_id='$topic_id'");
$topic=$get_topic->fetch_assoc();
?>
<form role="form" action="" id="edit_topic_form" method="post">
<div class="row">
<div class="col-md-6">عنوان الموضوع</div>
<div class="col-md-6"><input type="text" class="form-control" name="art_title" id="art_title" value="<?php echo $topic['art_title']; ?>"></div>
</div>
.........................
</form>
<?php
}
.php:
if($_POST){
$topic_array=array(
"art_title"=>$_POST['art_title'],
"art_subtitle"=>$_POST['art_subtitle'],
"art_desc"=>$_POST['art_desc'],
"art_tags"=>$_POST['art_tags'],
"art_download"=>$_POST['art_download'],
"art_yt"=>$_POST['art_yt'],
"art_instructor"=>$_POST['art_instructor'],
"art_com_no"=>$_POST['art_com_no'],
"art_likes"=>$_POST['art_likes']
);
$update_topic=$db->query("update articles set
art_title='".$topic_array['art_title']."',
art_subtitle='".$topic_array['art_subtitle']."',
art_desc='".$topic_array['art_desc']."',
art_tags='".$topic_array['art_tags']."',
art_download='".$topic_array['art_download']."',
art_yt='".$topic_array['art_yt']."',
art_instructor='".$topic_array['art_instructor']."',
art_com_no='".$topic_array['art_com_no']."',
art_likes='".$topic_array['art_likes']."'
where art_id='$topic_id'
");
}
jquery:
$(document).ready(function(){
var request;
$("#edit_topic_form").submit(function(event){
if (request) {
request.abort();
}
var $form = $(this);
var $inputs = $form.find("input, select, button, textarea");
var serializedData = $form.serialize();
$inputs.prop("disabled", true);
request = $.ajax({
url: "dashboard.php",
type: "post",
data: serializedData
});
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
alert("تم تعديل الموضوع بنجاح");
});
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});
event.preventDefault();
});
})
尝试使用var_dump($_POST)
查看是否有任何 POST 数据。另外,请确保您的 AJAX 请求没有被重定向到其他页面(例如 301 重定向)。重定向将导致开机自检数据丢失。您可以通过 Firefox/Chrome 中开发者工具中的"网络"窗口观察您的 AJAX 请求。
至于数据库未更新,我将首先验证您的POST数据是否正确接收。然后,您可以查看数据库查询的情况(我建议从中检查PHP和/或MySQL错误日志以帮助缩小问题范围)