Ajax调用按钮PHP表单用id ++,如何计算最大值?

时间:2018-04-23 05:54:50

标签: php jquery mysql ajax

有一些方法可以将PHP的值返回到ajax吗?或者其他一些方式?

简单的jQuery调用:

$(document).ready(function(){
    var quizCount= 1;
    $("button").click(function(){
        quizCount = quizCount+1;
        $("#quiz").load("load-quiz.php",{
            quizNewCount : quizCount
        });
    });
});

PHP表单:

$quizNewCount = $_POST['quizNewCount'];

$sql= "SELECT* FROM quiz WHERE quizid =$quizNewCount";
$result = $conn->query($sql);
mysqli_set_charset($conn,"utf8");
if ($result->num_rows > $quizNewCount)
{
   return $quizNewCount=1;
}

我试试,但没有效果。谁知道怎么做?我不会编号,但会改变价值,因为数据库每个月都会增加。

2 个答案:

答案 0 :(得分:0)

使用ajax代替load。这是一个示例代码。使用GET代替POST。解释here

 $.ajax({
    type:  'GET',
    url:   'load-quiz.php',
    data:  {quizNewCount : quizCount},
    success: function (result) {
        console.log(result);
    }
});

答案 1 :(得分:0)

    // Ajax calling
     post_data = {quizNewCount : quizCount}
     $.ajax({
                method: 'post',
                dataType: 'json',
                data: post_data,
                url: 'load-quiz.php',
                success: function (data) {
                    result = data;
                    alert(result)
                }
    })

    // action part or processing part
    $quizNewCount = $_POST['quizNewCount'];

            $sql= "SELECT* FROM quiz WHERE quizid =$quizNewCount";
            $result = $conn->query($sql);
            mysqli_set_charset($conn,"utf8");

            if ($result->num_rows > $quizNewCount) {
            $quizNewCount=1;
//as dataType is json so we need to enocde the data in the json format before sending it
            echo json_encode($quizNewCount);
        }