[英]Ajax is not updating data
我有一個論壇,在該論壇中,允許用戶僅編輯和刪除他的評論,我定義了一個“編輯”按鈕,單擊鼠標即可放下模式,在該模式下,用戶可以訪問對於以前發送過的數據,我編寫了一個ajax來定位這些字段,並在用戶單擊“編輯”按鈕時對其進行更新,代碼完全有意義,但到目前為止該功能還沒有實現更清楚的是,用戶單擊,模態下降,他/她發布的任何內容都將出現在字段中,模態底部有一個“編輯”按鈕,用於更改和更新數據。 這是模態代碼:
<button id="btn-btnedit" class="btn btn-primary " data-toggle="modal" data-target="#myModal<?php echo $list['id']; ?>">
Edit <i class="fa fa-pencil-square-o"></i>
</button>
<!-- Modal -->
<div class="modal fade" id="myModal<?php echo $list['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<div class="container">
<form style="width: 550px;" action="" method="post" id="signin-form<?php echo $list['id']; ?>" role="form">
<input type="hidden" name="commentID" value="<?php echo $list['id']; ?>">
<div class="from-group">
<label for="title">Title: </label>
<input class="form-control" type="text" name="title" id="txttitle" value="<?php echo $list['title']; ?>" placeholder="Page Title">
</div>
<div class="from-group">
<label for="label">Label: </label>
<input class="form-control" type="text" name="label" id="txtlabel" value="<?php echo $list['label']; ?>" placeholder="Page Label">
</div>
<br>
<div class="from-group">
<label for="body">Body: </label>
<textarea class="form-control editor" name="body" id="txtbody" row="8" placeholder="Page Body"><?php echo $list['body']; ?></textarea>
</div>
<br>
<input type="hidden" name="editted" value="1">
<br>
<br>
<input type="submit" id="btnupdate" value="Edit">
</form>
</div>
</div>
如您所見,我已將“ edited”分配給“ name”屬性,該屬性隨后用於調用數據庫中的查詢,sql代碼如下:
case 'postupdate';
if(isset($_GET['editted'])){
$title = $_GET['title'];
$label = $_GET['label'];
$body = $_GET['body'];
$action = 'Updated';
$q = "UPDATE posts SET title ='".$title."', label = '".$label."', body = '".$body."' WHERE id = ".$_GET['commentID'];
$r = mysqli_query($dbc, $q);
$message = '<p class="alert alert-success"> Your Post Is Succesfully '.$action.'</p>' ;
}
這是ajax代碼片段;
$('#btnupdate').click(function() {
var tempTitle = $('#txttitle').val();
var tempLabel = $('#txtlabel').val();
var tempBody = $('#txtbody').val();
var tempUrl = "index.php?page=postupdate"+"&title="+tempTitle+"&label="+tempLabel+"&body="+tempBody+"&commentID=30&editted=1";
$.get(tempUrl);
});
我認為這部分代碼沒有任何進展,並且我缺少一些非常簡單的東西,任何考慮都值得贊賞:)
此(未經測試的代碼)可能與您應該執行的操作類似:
$('#btnupdate').click(function() {
var tempTitle = $('#txttitle').val();
var tempLabel = $('#txtlabel').val();
var tempBody = $('#txtbody').val();
var tempParams = {"page":"postupdate","title":tempTitle,"label":tempLabel,"body":tempBody,"commentID":30,"editted":1};
$.post("index.php",tempParams,function(data) {
alert(data);
});
});
更新
嘗試使用ajax而不是查看加載是否發生了一些錯誤
$.ajax( {url:"index.php",data:tempParams,type: "POST"} ).done(function() {
alert( "success" );
}).fail(function() {
alert( "error" );
}).always(function() {
alert( "complete" );
});`
更新
然后開始測試點擊處理程序是否起作用(請確定!):
$('#btnupdate')。click(function(){alert(“是的,至少按下了按鈕”);});
更新
開始測試腳本是否被執行,然后:
alert("yes at least the script gets executed");
$('#btnupdate').click(function() { alert("yes at least the button was pressed"); });
如果不是,那么您必須在某個地方出現JavaScript錯誤。 https://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers
如果是,則您的按鈕不會被JQuery捕獲(不知道為什么)
無論如何,它與ajax無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.