简体   繁体   English

jQuery AJAX调用不会更新数据库

[英]jQuery AJAX call won't update database

I've been learning how to update databases using jQuery and AJAX so you don't need to refresh the page when you submit something, but I'm a little bit stuck. 我一直在学习如何使用jQuery和AJAX更新数据库,因此您在提交内容时不需要刷新页面,但是我有点卡住了。

This example is quite simple - you click the "accept" button, and in the database, the column "accepted" is updated to be '1' instead of '0'. 此示例非常简单-单击“接受”按钮,然后在数据库中,“接受”列更新为“ 1”而不是“ 0”。 The jQuery function appears to be working correctly, but the value isn't updating. jQuery函数似乎可以正常工作,但是值未更新。 I've tried switching from data: 'id='+id to the JSON data: { id: data } approach, as well as just using .val() instead of .serializeArray() , but nothing seems to work. 我尝试从data: 'id='+id切换到JSON data: { id: data }方法,以及仅使用.val()而不是.serializeArray() ,但似乎没有任何效果。 Any advice? 有什么建议吗?

form.php form.php的

<form id='acceptGoal' method='post'>
<input type='hidden' name='id' value='$gid'>
<input type='button' id='submit'>Accept?</button>
</form>
/* $gid is the unique identifier of each individual item to be accepted */

script.js 的script.js

$('#submit').click( function() {
    var data = $("#id").val();

    $.ajax({
        type: "POST",
        url: "accept.php",
        datatype: "json",
        data: { id: data }
        });
    });

accept.php accept.php

require 'core/initialize.php';
$query=$db->prepare("UPDATE goals SET accepted=:a WHERE id=:i");
$query->bindParam(":a", $a=1);
$query->bindParam(":i", $_POST['id']);
if ($query->execute()) {
    echo "Done";
    }
else {
    echo "Something went wrong.";
    }

first,you have syntax error at the end, an extra comma there: 首先,您最后会出现语法错误,并在其中加了一个逗号:

 $.ajax({
        type: "POST",
        url: "accept.php",
        datatype: "json",
        data: { id: data }, //<--------  Here is mistake 
        });

second, your hidden field does not have id attribute with the value id : 第二,您的隐藏字段没有id属性,其值为id

<input type='hidden' name='id' value='$gid'> // no id attribute in it while you accessing in jquery

while you are accessing it: 当您访问它时:

var data = $("#id").val(); // this will return nothing as element with this id not exists

change it to: 更改为:

<input type='hidden' id='id' name='id' value='$gid'>

you have to do like this: 您必须这样做:

$.ajax({
            type: "POST",
            url: "accept.php",
            datatype: "json",
            data: { id: data } 
            });

and you should add success and error callback to check it ajax call was successful or error occured: 并且您应该添加成功和错误回调以检查ajax调用是否成功或发生错误:

$.ajax({
            type: "POST",
            url: "accept.php",
            datatype: "json",
            data: { id: data },
            success:function(response){
            alert(response);
            },
            error:function(response){
             alert("error");
            }  
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM