【问题标题】:How to select a radio button in PHP如何在 PHP 中选择单选按钮
【发布时间】:2021-04-08 02:55:22
【问题描述】:

我的 AJAX 视图中包含我的 PHP 文件。 所以我的问题是,当我尝试使用它们的值选择无线电输入时,我得到了我的 else 语句。我还有额外的 CSS 和 Javascript,但我认为它们不会造成价值。看起来单选按钮可能不适用于 PHP,所以如果我找不到解决方案,那么也许我应该尝试将它们转换为复选框。

编辑:我已经设法得到帖子的结果,它正在返回:[object OBJECT]

有没有办法为玩家帐户获取我检查的收音机的价值?

电台的 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;
                  
              }
              
          } 
            }
        } 

表格

 <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();
                })
        

【问题讨论】:

  • 哪个单选按钮有问题? (是付款方式吗?)
  • 现在我主要关注玩家账号,抱歉没有澄清。我以为我应该只在 createplayer radios 之前编写代码
  • 什么是 signupform_input() ?
  • 我做的一个函数。
  • 我的PHP真的很长,所以函数在最上面,所以没有包含

标签: javascript php html jquery mysqli


【解决方案1】:

在您的 AJAX 调用中,对于参数 accountcreatorAccount,发布数据的方式是错误的:

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

代替

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

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 2016-02-23
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2016-06-17
    • 2014-11-08
    相关资源
    最近更新 更多