php 函数的按钮在 ajax 调用后不起作用


Button for php function doesnt work after ajax call

我正在尝试在数据库中添加或减去一个 pts 字段,如果您按下按钮然后更新页面上的内容,我也有 pts 正在显示的字段,在具有自动刷新Ajax的计时器上。

我正在使用此脚本调用页面:

$(document).ready(function(){
        var timer, delay = 1000;                // 1 second(s) counted in milliseconds
        var Pts = '';                          //ID value of users answer
        var info = {'userPts': Pts};           //naming POST for php
        timer = setInterval(function(){
            $.ajax({
                url: "inc/ans.php",            //sends to ans page where php function updates DB
                type: "POST",
                data : info,                   //stuff being sent to php
                success:function(data)
                {
                    $("div #ans").html(data);  //Retrieves answers made by user
                }
            });
        }, delay);

我的问题是当我进行此调用时,当前单击按钮的唯一方法是手动刷新整个页面,然后我能够单击按钮并更新用户 pts 值。我可以做一个或另一个,但不能同时做,我可以让它自动刷新内容,但我不能让按钮按下完成操作。我可以按下按钮,但我不能让内容自动刷新。

要加减的PHP函数大致相同,如下所示:

    function addPoint($mysqli, $id)
    {
        $stmt = $mysqli->prepare('
        UPDATE tbl_user 
        SET user_pts = user_pts + 1
        WHERE user_id = ?');
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $rows = $stmt->affected_rows; 
        $stmt->close();
        if($rows == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

这两个函数之间的唯一区别是它的 -1 或 +1。关于为什么会发生这种情况的任何想法?这是干扰PHP函数Ajax调用,反之亦然?

编辑:这是使用按钮向教师显示的代码

    function getScore_I($mysqli)
    {
        $stmt = $mysqli->prepare('
        SELECT user_name, user_pts, user_id
        FROM tbl_user');
        $stmt->execute();
        $stmt->bind_result($user, $pts, $id);
        $O ="";
        $x = 1;
        $O.="<table style='"text-align:center;width: 100%'"><form action=".$_SERVER['PHP_SELF']." method='"POST'">";
        while($stmt->fetch())
        {
            if($x%2)
            {
                $O.= "<tr style='"text-align:center;width: 100%'">
                        <td>".$user."</td>
                        <td>
                            <button name='"userIDmiunus'" type='"submit'" value='"".$id."'">-</button>
                        </td>
                        <td>".$pts."</td>
                        <td>
                            <button name='"userIDplus'" type='"submit'" value='"".$id."'">+</button>
                        </td>
                    </tr>";
                $x++;
            }
            else
            {
                $O.= "<tr style='"text-align:center;width: 100%'">
                        <td>".$user."</td>
                        <td>
                            <button name='"userIDmiunus'" type='"submit'" value='"".$id."'">-</button>
                        </td>
                        <td>".$pts."</td>
                        <td>
                            <button name='"userIDplus'" type='"submit'" value='"".$id."'">+</button>
                        </td>
                    </tr>";
                $x++;
            }
        }
        $O.= "</form></table>";
        // close statement
        $stmt->close();
        return $O;
    }

按钮元素是否包含在通过 AJAX 引入的代码中?如果是这样,您需要使用 .on() ,如:

$(document).on('click', '.buttonClassName', function(){
    //do your click code here
});

查看您的代码,看起来您只是在做一个.load(),这是$.ajax()的快捷方式版本。我也更喜欢在所有情况下都使用$.ajax() - 使用$.load()$.get()$.post()的结构不如完整的$.ajax()

当新数据通过.html()注入到DOM中时,任何控件都不会绑定到javascript代码,因为绑定代码在注入之前就已经运行了。两种解决方案:您还可以注入一些额外的javascript并重新创建这些绑定 - 这意味着DOM中有很多重复的代码 - 或者您可以使用.on()它适用于当前DOM中的元素,也适用于注入DOM的所有未来元素具有该特定类或ID或您使用的任何jQuery选择器。

另一种可能性是您需要使用 AJAX 来访问您的 PHP 函数。请参阅这篇文章以及它引用/链接到的有关 AJAX 的帖子。查看示例。


是的,您可以在.on(),内调用另一个 AJAX 函数,这可能是您需要做的。

回想一下,AJAX 只是一种在网页呈现运行 PHP 代码的方法,而无需刷新/离开当前页面。

代码可能如下所示:

$(document).on('click','.buttonClass', function(){
    var Pts = '';                          //ID value of users answer
    var uid = $('hidden_input_where_you_stored_userID').val();
    var info = {'userID': uid, 'userPts': Pts};           //naming POST for php
    $.ajax({
        url: "inc/another_file.php",
        type: "POST",
        data : info,
        success: function(data) {
            //whatever you need to do after updating point score
        }
    });
});

another_file.php:

<?php
    $id =  $_POST['userID'];
    $pts = $_POST['userPts']; 
    //do your DB update