Ajax没有更新数据
Ajax is not updating data
我有一个论坛,在这个论坛中,用户只允许编辑和删除他的评论,我定义了一个"编辑"按钮,只需点击鼠标就可以放下一个模态,在这个模态中,用户可以访问他/她以前发送过的数据,我写了一个ajax来针对这些字段,并在用户点击"编辑"键时更新它们,代码完全有意义,但到目前为止,为了更清楚地表明,该功能还没有,用户点击,modal关闭,他/她发布的任何内容都会出现在字段中,modal底部有一个"编辑"按钮,负责更改和更新数据。这是模式代码:
<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或get无关!
相关文章:
- 平均值:无法将数据更新到数据库
- d3在数据更新时错误地附加了dom元素
- 使用角度传递的数据更新模态
- jQuery使用XML数据更新UL列表
- ng表DOM未使用数据更新进行更新
- 在线/离线数据更新移动最佳实践
- 使用 预测 API 数据更新的 HTML 模板
- JQuery 数据表列数据更新工作太慢
- 使用Plotly中的新数据更新图形的高性能方法
- 使用新数据更新 D3 饼图.json
- jQuery - 使用相同的数据更新 2 个或多个表单字段
- 如何在数据更新时更新可重用的 d3.js 图形
- 组合框/下拉框在数据更新后未更新
- 在 JavaScript 中的数据更新后,我的 DOM 对象不会更新
- 如何使用来自外部源的数据更新 ng 模型
- 主干.js:使用不同的数据更新集合
- 对数据库进行 AJAX 编辑时,我是否应该立即使用新数据更新接口
- 计划脚本以使用外部数据更新数据库
- 使用从 MongoDB 获取的新数据更新对象属性
- 如何每 n 分钟使用数据更新一次 Web 应用程序