通过ajax更新数据库,而不刷新页面codeigniter

update db by ajax without refreshing the page codeigniter

本文关键字:刷新 codeigniter ajax 更新 数据库 通过      更新时间:2023-09-26

我正试图通过查询和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;
}