$_SESSION 未使用 Ajax 更新

Posted

技术标签:

【中文标题】$_SESSION 未使用 Ajax 更新【英文标题】:$_SESSION is not updating with Ajax 【发布时间】:2021-04-20 02:14:28 【问题描述】:

我正在尝试更新会话 playerPlan,但它没有给我更新的计划值,而是给我 1,这是预设值。我怀疑 php 没有识别我的帖子,但我的其他 PHP 文件正在识别我的文本输入帖子。对于我的复选框,PHP 也无法识别该帖子,因此我不得不使用 jquery。所以现在我有一个 ajax,它只是在控制台中返回会话的值并将其更新到我的数据库中。我在本地主机上使用它:XAMPP。我现在不关心 SQL 注入,但我很高兴有一个密码哈希教程,因为我学过 md5。我没有包含我插入的 PHP 文件。

FORM: 

  <div class="container" style="margin: 50px;">
          <form action="actionSignUp.php" method="post" id="signupForm" class="signupForm">
          <h2>Signup</h2>
            <input type="hidden" name="loginActive" id="loginActive" value="0">
            <br>
        <div class="form-group row">
        <label for="username" class="col-lg-16 col-form-label">Username:</label>
        <div class="col-lg-8">
        <input type="text" class="form-control signupInput" name="username" id="username" aria-describedby="username" autocomplete="username" autofocus required>
        </div>
        <p><span class="error"><?php echo $usernameError;?></span><p>
            </div>
      <div class="form-group row">
        <label for="password" class="col-lg-16 col-form-label">Password:</label>
        <div class="col-lg-8">
        <input type="password" class="form-control signupInput" id="Password" name="password" autocomplete="current-password" required>
          </div>
          <p><span class="error"><?php echo $passwordError;?></span><p>
        </div>
        
        <h4>Account Type <i class="far fa-question-circle info" data-toggle="tooltip" data-placement="right" title="Whenever you have all free accounts checked, payment method should be hidden, but if not then click on any paid account and then click back!" ></i></h4>
        <p>Visit <a href="pricing.php">Plan</a> for pricing details.</p>
          <div class="input-group-prepend row">
              <input type="button" class="dropdown-item col-md-4 active dropdownI plan" value="Player: Free Account $0.00/Mo" class="FA" id="dfreePlayerAccount" name="fa">
              <input type="button" class="dropdown-item col-md-4 dropdownI plan" value="Player: Pro Account $5.99/Mo" class="FA" id="dproPlayerAccount" name="pa">
              <input class="dropdown-item col-md-4 dropdownI plan" value="Player: Premium Account $9.99/Mo" class="FA" id="dpremiumPlayerAccount" name="pra">
          </div>
            <br>
          <input type="radio" id="freePlayerAccount" class="free playAccount readonly" name="account" value="1" checked>
          <label for="freePlayerAccount">Player: Free Account $0.00/Mo</label><br>
          <input type="radio" id="proPlayerAccount" class="paid playAccount readonly" name="account" value="2">
          <label for="proPlayerAccount">Player: Pro Account $5.99/Mo</label><br>
          <input type="radio" id="premiumPlayerAccount" class="paid playAccount readonly" name="account" value="3">
          <label for="premiumPlayerAccount">Player: Premium Account $9.99/Mo</label><br>
          <hr>
             <h5>Optional:</h5>
             <div class="row">
              <input type="button" class="dropdown-item col-sm-6 CA" value="Creator: Pro Account $9.99/Mo" name="pca" id="dproCreatorAccount">
              <input type="button" class="dropdown-item col-sm-6 CA" value="Creator: Premium Account $14.99/Mo" id="dpremiumCreatorAccount" name="prca">
              </div>
        <br>
          <input type="radio" id="creatorProAccount" class="paid creatorAccount readonly" name="creatorAccount" value="5">
          <label for="creatorProAccount">Creator: Pro Account $9.99/Mo</label><br>
          <input type="radio" id="creatorPremiumAccount" class="paid creatorAccount readonly" name="creatorAccount" value="6">
          <label for="creatorPremiumAccount">Creator: Premium Account $14.99/Mo</label><br>
            <hr>
        <div class="hiddenPaymentMethod"> <h5>Payment Method</h5>
          <input type="radio" id="Paypal" name="payment" value="Paypal" class="payment">
          <label for="Paypal">Paypal</label><br>
          <input type="radio" id="creditCard" name="payment" value="CreditCard" class="payment">
          <label for="creditCard">Credit Card</label><br>
          <input type="text" style="display:none;" name="creditCardNumber" id="creditCardNumber" placeholder="Card Number">
          <input type="radio" id="debitCard" name="payment" value="DebitCard" class="payment">
      <label for="debitCard">Debit Card</label>
           <input type="text" style="display:none;" name="debitCardNumber" id="debitCardNumber" placeholder="Card Number"> 
            <br></div>
            <br>
        <input type="checkbox" id="termsAndConditions" class="conditions" name="termsandconditions" value="0">
        <label for="termsAndConditions"> I have read and agreed to the Terms and Conditions <span data-toggle="modal" data-target="#exampleModal"><i class="far fa-question-circle questionMark"></i></span></label>
        <p id="errors"></p>
        <p id="tacError" style="color:red"></p>
        <input type="submit" class="btn btn-primary" name="signupButton" id="signUpButton" value="Submit">
    </form>
</div>

AJAX:

 $(document).ready(function()
                  $('#signupForm').on('submit' , function(e)
                    e.preventDefault();
                    var plan = $('.plan').val();
                    $.ajax(
                      method: "POST",
                      url: "ChangeUserPlan.php",
                      data: plan : plan + "fa=" + $("#dfreePlayerAccount").val() + "&pa=" + $("#dproPlayerAccount").val() + "&pra=" + $("#dpremiumPlayerAccount").val() 
                    ).done(function(updated)
                        console.log(updated);
                    ).fail(function(xhr, textStatus, errorThrown) 
                         console.log("Error Requesting. Please Try Again Later.");
                    );
                  );
                );

会话更新 PHP 文件:

    //session_start() is already stated in the signup.php file.
    require('signup.php');
    $link = mysqli_connect("****", "****", "****", "****");
    
     
    if(mysqli_connect_error()) 
         
         die("Couldn't connect to the database. try again later.");
         
      
 
      $query = "SELECT * FROM `users`";

      if($result = mysqli_query($link, $query)) 
          
          $row = mysqli_fetch_array($result);
          
    
    $_SESSION['playerPlan'] = "1";
            if(isset($_REQUEST['fa'])) 
                $_SESSION['playerPlan'] = "1";
            
            if(isset($_REQUEST['pa'])) 
                $_SESSION['playerPlan'] = "2";
            
            if(isset($_REQUEST['pra'])) 
                $_SESSION['playerPlan'] = "3";
            
    
    if($_SERVER["REQUEST_METHOD"] == "POST") 
        echo $_SESSION['playerPlan'];
     else 
        echo "no";
    
?>

编辑:我使用的是最新版本的 PHP。

【问题讨论】:

请参阅 this question 以获取 password_hash()。 不要使用 MD5 存储密码 回应查询、会话和帖子:INSERT INTO users (username, password, plan) VALUES ('bbbbbbbb', 'hebf', '1') 1 Array ( [username] => bbbbbbbb [password] => hebf [termsandconditions] => 1 [fa] => 玩家:免费账户 $0.00/月 [pa] => 玩家:专业账户 $5.99/月 [pra] => 玩家:高级帐户 $9.99/Mo [pca] => 创建者:专业帐户 $9.99/Mo [prca] => 创建者:高级帐户 $14.99/Mo [帐户] => 1 [loginActive] => 0 ) 我为此检查了值 2,但它给了我帐户名称的值 1,因为我有检查收音机的按钮。 @PalDhillon,我对某些部分进行了硬编码,以便您轻松理解。 【参考方案1】:

您的 ajax 函数未正确请求。

您的 ajax 函数应如下所示。

 $(document).ready(function()
                  $('#signupForm').on('submit' , function(e)
                    e.preventDefault();
                    var plan = $('.plan').val();
                    var playAccount = $('input[name="account" ]:checked').val();
                    var key = "";
                    if (playAccount == "1") 
                      key = "fa";
                      plan = $("#dfreePlayerAccount").val();
                     else if (playAccount == "2") 
                      key = "pa";
                      plan = $("#dproPlayerAccount").val();
                     else if (playAccount == "3") 
                      key = "pra";
                      plan = $("#dpremiumPlayerAccount").val();
                    
                    var data = 
                      plan: plan
                    ;
                    data[key] = plan;
                    $.ajax(
                      method: "POST",
                      url: "ChangeUserPlan.php",
                      data: data
                    ).done(function(updated)
                        console.log(updated);
                    ).fail(function(xhr, textStatus, errorThrown) 
                         console.log("Error Requesting. Please Try Again Later.");
                    );
                  );
                );

【讨论】:

另外,我为什么要那样做ajax。有原因吗?偏好? 如果你这样要求,你的帖子参数将是plan: xxxfa:yyy&amp;pa:zzz&amp;pra:xxx 虽然有一个问题,控制台显示了正确的会话,但查询插入了它之前的会话。我知道这可能很难理解,所以我给你举个例子。假设用户 1 选择高级帐户。用户 1 获得了他之前的会话值。然后假设用户 2 出现并选择免费,然后查询插入来自用户 1 的高级会话而不是用户 2 的免费会话。

以上是关于$_SESSION 未使用 Ajax 更新的主要内容,如果未能解决你的问题,请参考以下文章

PHP中为啥$_SESSION语句同时用时,临时文件里有一个是空的?

使用ajax上下文不能解决范围问题

PHP执行Session与前端JS之间的关系

使用 Ajax 未更新第二个表

token的作用以及ajax中传递token的几种方法

sess_destroy() 无法正常工作