0

I have my PHP file included in my views for AJAX. So my problem is that when I try to select the radio inputs with their values I instead get my else statement. I also have my additional CSS and Javascript, but I don't think they are causing the value. It looks like radio buttons may not be made for PHP so if I may not find a solution then maybe I should try to convert them into a checkbox.

Edit: I have managed to get the result of the post and it's returning: [object OBJECT]

Is there a way to get the value of my checked radios for the player account?

Radio's PHP

$usernameError = "";
        $passwordError = "";
        $termsandconditionsError = "";
        $playAccount = "";
        $creatorAccount = "";
        $account = "";
        $error = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
          if (empty($_POST["username"])) {
            $usernameError = "Username is required.";
            echo $usernameError;
          } else {
            $username = signupform_input($_POST["username"]);
          }
          if (empty($_POST["password"])) {
            $passwordError = " Password is required.";
            echo $passwordError;
          } else {
            $password = signupform_input($_POST["password"]);
          }  
        if(!empty($_POST['account'])) {
            $playAccount = $_POST['account'];
        }
         if(!empty($_POST['creatorAccount'])) {
            $creatorAccount = $_POST['creatorAccount'];
        }
        if(isset($_POST['account'])) {
        if(!empty($_POST['account'] || $_POST['creatorAccount'])) {
            $account = $_POST['account'].', '.$_POST['creatorAccount'];
        }
        }   
        
          if(isset($_POST['loginActive'])) {
          if($_POST['loginActive'] == "0" && $usernameError == "" && $passwordError == "" && $termsandconditionsError == "") {
              
              $query = "SELECT * FROM users WHERE username = '". mysqli_real_escape_string($link, $_POST['username'])."' LIMIT 1"; 
              $result = mysqli_query($link, $query);
              if(mysqli_num_rows($result) > 0) {
                    
                  $error = "That username is already taken.";
                  echo $error;
              } else {
                  
                  echo "<p style='color: green'>Hi</p>";
                  $query = "INSERT INTO `users` (`username`, `password`, `plan`) VALUES ('". mysqli_real_escape_string($link, $_POST['username'])."', '". mysqli_real_escape_string($link, $_POST['password'])."', '". mysqli_real_escape_string($link, $account)."')";
                  echo $query;
                  
              }
              
          } 
            }
        } 

Form

 <div class="container" style="margin: 50px;">
          <form 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>
          <input type="radio" id="freePlayerAccount" class="free playAccount" name="account" value="1" checked>
          <label for="freePlayerAccount">Player: Free Account $0.00/Mo</label><br>
          <input type="radio" id="proPlayerAccount" class="paid playAccount" name="account" value="2">
          <label for="proPlayerAccount">Player: Pro Account $5.99/Mo</label><br>
          <input type="radio" id="premiumPlayerAccount" class="paid playAccount" name="account" value="3">
          <label for="premiumPlayerAccount">Player: Premium Account $9.99/Mo</label><br>
          <hr>
          <input type="radio" id="creatorFreeAccount" class="creatorAccount" name="creatorAccount" value="4" class="free">
          <label for="creatorFreeAccount">Creator: Free Account $0.00/Mo</label><br>
          <input type="radio" id="creatorProAccount" class="paid creatorAccount" name="creatorAccount" value="5">
          <label for="creatorProAccount">Creator: Pro Account $9.99/Mo</label><br>
          <input type="radio" id="creatorPremiumAccount" class="paid creatorAccount" 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="button" class="btn btn-primary" name="signupButton" id="signUpButton" value="Submit">
    </form>
</div>
 

Jquery

  $("#signUpButton").click(function() { 
                $.ajax({
                    type: "POST",
                    url: "actionSignUp.php",
                    data: "username=" + $("#username").val() + "&password=" + $("#Password").val() + "&termsandconditions=" + $("#termsAndConditions").val() + "&account=" + $(".playAccount") + "&creatorAccount=" + $(".creatorAccount") + "&loginActive=" + $("#loginActive").val()
                }).done(function(result)  {
                        $("#errors").html(result);
                    }).fail(function(xhr, textStatus, errorThrown) {
                    
                        alert("Error Requesting. Please Try Again Later.");
                    
                    });

                }); 
                
                $('input:checkbox').change(
                    function(){
                        if ($(this).is(':checked')) {
                            $(this).val("1");
                        } else {
                            $(this).val("0");
                        }
                    });
                
                 $("#signUpButton").click(function(){
                    
                    if($(".conditions").val() == "0") {
                        $("#tacError").html("Terms and Conditions are required");
                    } else {
                        
                        $("#tacError").html("");
                    }
                })
               
                $('input:radio[name="account"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '2') {
                            $(".hiddenPaymentMethod").show();
                        }
                    });
                    
                $('input:radio[name="account"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '3') {
                            $(".hiddenPaymentMethod").show();
                        }
                    });
                $('input:radio[name="account"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '1') {
                            $(".hiddenPaymentMethod").hide();
                            $("#Paypal").removeAttr('checked');
                        }
                    });
                 $('input:radio[name="creatorAccount"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '6') {
                            $(".hiddenPaymentMethod").show();
                            $("#Paypal").attr('checked', 'checked');
                        }
                    });
                    
                $('input:radio[name="creatorAccount"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '5') {
                            $(".hiddenPaymentMethod").show();
                            $("#Paypal").attr('checked', 'checked');
                        }
                    });  
            
            $('input:radio[name="account"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '2') {
                            $("#Paypal").attr('checked', 'checked');
                        }
                    });
                $('input:radio[name="account"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == '3') {
                            $("#Paypal").attr('checked', 'checked');
                        }
                    });
                    
                $('input:radio[name="payment"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == 'CreditCard') {
                            $("#creditCardNumber").show();
                        }
                    });
                
                $('input:radio[name="payment"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == 'Paypal' || $(this).val() == 'DebitCard') {
                            $("#creditCardNumber").hide();
                        }
                    });
            
                $('input:radio[name="payment"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == 'DebitCard') {
                            $("#debitCardNumber").show();
                        }
                    });
                
                $('input:radio[name="payment"]').change(
                    function(){
                        if ($(this).is(':checked') && $(this).val() == 'Paypal' || $(this).val() == 'CreditCard') {
                            $("#debitCardNumber").hide();
                        }
                    });
                 $(function () {
                  $('[data-toggle="tooltip"]').tooltip();
                })
        
Pal Dhillon
  • 93
  • 10
  • Which radio button you have problem ? (is it payment method?) – Ken Lee Jan 01 '21 at 01:55
  • Right now I'm focusing on the player account, sorry for not clarifying. I thought I only should the code till the createplayer radios – Pal Dhillon Jan 01 '21 at 01:56
  • What is signupform_input() ? – Ken Lee Jan 01 '21 at 02:06
  • A function I made. – Pal Dhillon Jan 01 '21 at 02:09
  • My PHP is really long, so the function is at the top, so it's not included – Pal Dhillon Jan 01 '21 at 02:10
  • I have included the most important parts – Pal Dhillon Jan 01 '21 at 02:10
  • are you using ajax ? (if yes, can you post the part on ajax too ?) – Ken Lee Jan 01 '21 at 02:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226682/discussion-between-ken-lee-and-pal-dhillon). – Ken Lee Jan 01 '21 at 03:52
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Jan 01 '21 at 13:38
  • I was just testing, so I was not using a password hash so that I can log back in. But afterwards I will. – Pal Dhillon Jan 02 '21 at 20:14
  • You are retrieving account value as "&account=" + $(".playAccount") ...but I can not see any element having class name as "playAccount" ... this could be an issues in your case. – Kartik Goyal Jan 02 '21 at 20:38
  • No, I have a class named that in the first 3 radios – Pal Dhillon Jan 02 '21 at 20:40
  • Could it be that I need to include each other in both files like the PHP file included in the form file and the form file included in the PHP file because right now the PHP file is included in the form file. And because of it, the PH doesn't know the values? – Pal Dhillon Jan 02 '21 at 20:49

1 Answers1

1

In your AJAX call posting data is wrong way for params account and creatorAccount:

data: "username=" + $("#username").val() + "&password=" + $("#Password").val() + "&termsandconditions=" + $("#termsAndConditions").val() + "&account=" + $(".playAccount:checked").val() + "&creatorAccount=" + $(".creatorAccount:checked").val() + "&loginActive=" + $("#loginActive").val()

Instead of

data: "username=" + $("#username").val() + "&password=" + $("#Password").val() + "&termsandconditions=" + $("#termsAndConditions").val() + "&account=" + $(".playAccount") + "&creatorAccount=" + $(".creatorAccount") + "&loginActive=" + $("#loginActive").val()
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29