$_SESSION is not updating with Ajax

115
January 15, 2021, at 1:20 PM

I'm trying to update the session playerPlan but instead of giving me the updated plan value, it gives me 1, which was the preset value. I suspect that the PHP is not recognizing my post but my other PHP file was recognizing my text input posts. For my checkbox as well, the PHP didn't recognize the post, so I had to use jquery. So right now I have an ajax which just returns the value of the session in the console and updates it into my database. I'm using this on localhost: XAMPP. I don't care about SQL injections right now but I'd be happy for a password hash tutorial, as I was taught md5. I didn't include my inserting PHP file.

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!" height="16px"></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.");
                    });
                  });
                });

SESSION Updating PHP file:

    //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";
    }
?>
Answer 1

Your ajax function does not reuqest correctly.

Your ajax function should look like the below one.

 $(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.");
                    });
                  });
                });
READ ALSO
working but need to get that piece from mysql

working but need to get that piece from mysql

I have the following code which is working as expected,

121
error: selenium.common.exceptions.TimeoutException: Message:

error: selenium.common.exceptions.TimeoutException: Message:

in the code below, I tried to log in to a website using selenium But I get this error seleniumcommon

117
jetpack compose not responding to keyboard input on emulator

jetpack compose not responding to keyboard input on emulator

The software keyboard works while keyboard has no response

204
Hexagon Grid CSS - All hexagons change size when each of them is clicked

Hexagon Grid CSS - All hexagons change size when each of them is clicked

I have 8 hexagons made purely with HTML and CSS and the code is taken from this exampleHowever, I have inserted my own images in them

48