dqjo4340 2014-06-15 16:47
浏览 41
已采纳

jQuery AJAX调用不会更新数据库

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.

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'. The jQuery function appears to be working correctly, but the value isn't updating. 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. Any advice?

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

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

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

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.";
    }
  • 写回答

1条回答 默认 最新

  • douhan9467 2014-06-15 16:50
    关注

    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:

    <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({
                type: "POST",
                url: "accept.php",
                datatype: "json",
                data: { id: data },
                success:function(response){
                alert(response);
                },
                error:function(response){
                 alert("error");
                }  
                });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c