Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用ajax后,php函数的按钮不起作用_Javascript_Php_Jquery_Ajax - Fatal编程技术网

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

Javascript 调用ajax后,php函数的按钮不起作用,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图在数据库中添加或减去一个pts字段,因为如果您按下一个按钮,然后更新页面上的内容,我还可以在计时器上使用Ajax自动刷新pts显示的字段 我正在使用此脚本调用页面: $(document).ready(function(){ var timer, delay = 1000; // 1 second(s) counted in milliseconds var Pts = '';

我试图在数据库中添加或减去一个pts字段,因为如果您按下一个按钮,然后更新页面上的内容,我还可以在计时器上使用
Ajax
自动刷新pts显示的字段

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

$(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;
        }
    }
$(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
        }
    });
});
这两个函数之间唯一的区别是它的-1或+1。你知道为什么会这样吗?这是对
PHP
函数或vic的干扰。反之亦然

编辑:这是通过按钮向讲师显示的代码

    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;
    }
函数getScore_I($mysqli) { $stmt=$mysqli->prepare($stmt) 选择用户名、用户名、用户名 来自tbl_用户’); $stmt->execute(); $stmt->bind_result($user,$pts,$id); $O=“”; $x=1; $O.=”; 而($stmt->fetch()) { 如果($x%2) { $O.=” “$user。” - “$pts。” + "; $x++; } 其他的 { $O.=” “$user。” - “$pts。” + "; $x++; } } $O.=”; //结束语 $stmt->close(); 退还$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的post参考/链接。看看这些例子


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

回想一下,AJAX只是一种在呈现网页之后运行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;
        }
    }
$(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
        }
    });
});
另一个文件.php:

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

通过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的post参考/链接。看看这些例子


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

回想一下,AJAX只是一种在呈现网页之后运行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;
        }
    }
$(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
        }
    });
});
另一个文件.php:

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

为什么要使用计时器?…单击时是否希望通过ajax执行php功能?…如果是,请尝试使用单击事件而不是计时器。我只希望有一个固定的计时器,当答案添加到数据库中或在讲师端为该超级用户分配新的pts值时,该计时器将自动更新页面,你为什么使用计时器?…你想在单击时通过ajax执行php函数吗?…如果是,尝试使用单击事件而不是计时器。我只想有一个固定的计时器,可以自动升级