使用AJAX配置提交按钮,以检查增量变量的值


Configuring a submit button using AJAX that checks for the value of an incremented variable

我试图正确使用AJAX来显示一个单独的PHP文件中的增量值,但我对代码有点不确定。这就是它的样子:

$("#submit").click(function()
    {
      $.ajax({
      type: 'POST',
      url: 'info.php',
      dataType: 'json',
      data: {$_SESSION['totalCorrect'}
      success: function()
      {
        if ($_SESSION['totalCorrect'] >= 8 && $_SESSION['totalCorrect'] <= 10) 
        {
          window.location.replace('success.php');
        }
        else
        {
          window.location.replace('retake.php');
        }
      }
      });
    });

该值存储在info.php中,我正试图从该文件中提取该值,但我不确定如何编写AJAX语法。我确信这个data: {$_SESSION['totalCorrect'}代码不正确。

我可以显示递增的值,这样我至少知道变量正在递增,但我现在想做的是使用该变量检查它们是否通过。如果他们这样做了,那么他们会被重定向到success.php。如果没有,则将它们发送到retake.php

编辑:info.php

if (empty($_SESSION["totalCorrect"]))
{
    $_SESSION["totalCorrect"] = 0;
}
else 
{
    $totalCorrect =  $_SESSION["totalCorrect"];
}
foreach ($correctAns as $key => $answer)
{
    if (!empty($_POST[$key]))
    {
        if ($_POST[$key] == $answer)
        {
            echo $correct[$index];
            $_SESSION["totalCorrect"]++;    
        }
        else if($_POST[$key] != $answer)
        {
            echo $incorrect[$index];
        }
        $_SESSION[$key] = true;
    }
    $index++;
};

您还没有发布所有代码,所以我只能回答一个问题。

第一个
您正在混合php和javascript。您无法访问javascript中的php变量,除非您将它们作为字符串分配给输出html或通过ajax发布它们——这就是您想要做的。

因此,让我们来看看您的jQuery ajax:

$("#submit").click(function() {
  $.ajax({
     type: 'POST',
     url: 'info.php',
     // dataType: 'json', // I deactivated this to recieve a string only
     // in data you should get the answers the user gave - another topic
     data: {question1: '42', question2: 'Douglas'}, // that ',' was missing
     // you'll get whatever you echo in info.php as 'result' as parameter 
     // in this success-callback-function (as result [json]):
     success: function(result) {
        // now you can check what the result is:
        console.log(result); // make it visible to you for debugging
        if (parseInt(result) >= 8 && parseInt(result) <= 10) {
          console.log('redirecting to SUCCESS');
          //window.location.replace('success.php'); // uncomment when working
        }
        else {
          console.log('redirecting to RETAKE');
          //window.location.replace('retake.php'); // uncomment when working
        }
     }
  });
});

现在让我们调整您的info.php:

if (empty($_SESSION["totalCorrect"]))
{
    $_SESSION["totalCorrect"] = 0;
}
else 
{
    $totalCorrect =  $_SESSION["totalCorrect"];
}
foreach ($correctAns as $key => $answer)
{
    // remember, that in $_POST you'll get everything you've put in 'data' before!
    if (!empty($_POST[$key]))
    {
        if ($_POST[$key] == $answer) // better change to === if you know what data-types you'll get (and google the difference)
        {
            //echo $correct[$index]; // do not echo anything else but the result
            $_SESSION["totalCorrect"]++;    
        }
        else if($_POST[$key] != $answer)
        {
            //echo $incorrect[$index];
        }
    $_SESSION[$key] = true;
    }
    // $index++; // we don't need that, do we??
};
// here's the actual response/result: 
// All you echo will be in 'result' of your ajax succes-callback-function
echo $_SESSION["totalCorrect"];

我不知道你的表单是什么样子的,也不知道所有页面是如何设置的,所以我无法提供完整的代码——这也远远超出了问题的范围
但这应该让您了解如何通过ajax从php获取are变量。

进一步信息:使用JavaScript获取表单数据,并使用Ajax 发送数据

关于json的有用内容:
http://www.w3schools.com/js/js_json.asp
http://php.net/manual/en/function.json-encode.php