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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化