通过ajax更新数据库,而不刷新页面codeigniter
update db by ajax without refreshing the page codeigniter
我正试图通过查询和ajax更新表中的一行。
我成功了,但我有一个问题。
我拥有的页面是一个只有一个输入的表单;输入是DB中的行的CCD_ 1。提交表单后,我会更新行,但如果我在输入中插入另一个id
,那么在再次刷新页面之前,它不会工作。意思是:我必须再次刷新页面才能更新表中的另一行。我正在使用代码点火器。
这是我的代码:
<form method="POST" action="" >
<fieldset data-role="controlgroup">
<input type="text" name="id" id="id" value=""
<input type="submit" name="submit-choice-a1" id="submit-choice-b1" value="submit" />
</fieldset>
</form>
JS为:
<script type="text/javascript">
$(function() {
$("#submit-choice-b1").click(function(e) {
var id = $('#id').val();
var result = confirm("are you sure ?");
if (result==true) {
$.ajax({
url: "the url that have the page with the update function",
success: function(xhr){
alert('updated');
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
}
});
}
});
});
</script>
我看到了两种方法:
1) 不要使用<form>
标签,只需获取信息并在数据参数中使用jQueryajax发送即可。
$.ajax({
url: "the url",
type: "post",
data: {
id: 68,//Your field id/class, got it with $(selector).attr("id") or $(selector).attr("class");
anotherKey: anotherValue
etc...
}
});
2) 使用onsubmit属性修改表单
<form method="post" onsubmit="return sent();">
在sent()函数的末尾,您需要添加"return false"来告知表单(不要重新加载页面)
function sent() {
..your ajax implementation here..
return false;
}
相关文章:
- 强制模板刷新ember.js
- 如何通过ajax刷新JSF填充的javascript变量
- 如何解决Yii中的页面刷新问题
- Jquery提交表单而不刷新
- 刷新页面后会出现警报
- 刷新后保留对网页的更改
- 如何在不刷新页面的情况下更新显示框
- 重定向时角度刷新浏览器
- 如何防止jQueryonclick事件中的Ruby方法在页面刷新时执行
- Javascript,输出结果后页面不断刷新
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 刷新父窗口后无法关闭窗口
- 刷新页面时hasClass不起作用
- Bootstrap模态主体:Json在上传Codeigniter后刷新
- 使用 Codeigniter 3 关闭警报 javascript 而不刷新页面
- CodeIgniter:如何创建不刷新的菜单
- 通过ajax更新数据库,而不刷新页面codeigniter
- 添加帖子并在不刷新页面的情况下显示Codeigniter
- Codeigniter:使用jQueryajax提交表单数据,无需刷新页面
- Codeigniter:如何在不刷新的情况下将数组从控制器传递到视图