1

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

Edit: I'm using the latest version of PHP.

RobC
  • 22,977
  • 20
  • 73
  • 80
Pal Dhillon
  • 93
  • 10
  • See [this question](https://stackoverflow.com/q/30279321/14853083) for [password_hash()](https://www.php.net/manual/en/function.password-hash.php). Do **not** use MD5 to store passwords – Tangentially Perpendicular Jan 15 '21 at 02:09
  • Echoing the query, session, and post: INSERT INTO `users` (`username`, `password`, `plan`) VALUES ('bbbbbbbb', 'hebf', '1') 1 Array ( [username] => bbbbbbbb [password] => hebf [termsandconditions] => 1 [fa] => Player: Free Account $0.00/Mo [pa] => Player: Pro Account $5.99/Mo [pra] => Player: Premium Account $9.99/Mo [pca] => Creator: Pro Account $9.99/Mo [prca] => Creator: Premium Account $14.99/Mo [account] => 1 [loginActive] => 0 ) – Pal Dhillon Jan 15 '21 at 02:12
  • I checked the value 2 for that but it's giving me the value 1 for account name, as I have buttons that check the radios. – Pal Dhillon Jan 15 '21 at 02:13
  • @PalDhillon, I've hard-coded some parts so that you can understand easily. – robdev91 Jan 15 '21 at 02:31

1 Answers1

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.");
                    });
                  });
                });
robdev91
  • 1,006
  • 1
  • 10
  • 18
  • Also, why should I do the ajax like that. Is there a reason? Preferability? – Pal Dhillon Jan 15 '21 at 02:20
  • 2
    If you request like that, your post param will be `plan: xxxfa:yyy&pa:zzz&pra:xxx` – robdev91 Jan 15 '21 at 02:29
  • There is one problem though, the console displays the right session but the query inserts the session that came before it. I know it might be hard to understand, so I'll give you an example. Let say user 1 picks premium account. User 1 gets the session value of what came before him. Then let say user 2 comes along and pick free, then the query inserts the premium session from user 1 instead of the free session for user 2. – Pal Dhillon Jan 17 '21 at 00:34