如何在提交按钮后保持我的单选按钮保持选中状态?

时间:2014-02-10 21:13:27

标签: html forms radio-button

我是新手编码并且需要选择两个选项的问卷...我可以选择选择并输出正确的答案,但是,选择的选项不会保留。如何在用户点击提交按钮后停留?我试图研究它并使用了几种不同的格式,但它仍然无法正常工作。不确定这是我的语法还是我没有正确的编码。 我已经提供了代码的副本。

    <form method="post" action="">
        <p><span class="error">* required field.</span></p>
        <p>What is the pay period reported by Company X:</p>
        <input type="radio" name="payPeriod" value="1" <?php if (isset($_POST['payPeriod']) && $_POST['payPeriod']=='biWeekly') echo ' checked="checked"';?> />Bi-Weekly
        <input type="radio" name="payPeriod" value="2" <?php if (isset($_POST['payPeriod']) && $_POST['payPeriod']=='semiMonthly') echo ' checked="checked"';?> />Semi-Monthly
        <span class="error">* <?php echo $payPeriodErr;?></span>
        <br/>
        <p>What is the clients response to pay period:<br/>
        (How often do you get paid, once every two weeks or twice a month?)</p>
        <input type="radio" name="pay_Period" value="1" <?php if (isset($_POST['pay_Period']) && $_POST['pay_Period']=='bi_Weekly') echo ' checked="checked"';?> />Every two weeks
        <input type="radio" name="pay_Period" value="2" <?php if (isset($_POST['pay_Period']) && $_POST['pay_Period']=='semi_Monthly') echo ' checked="checked"';?> />Twice a month
        <span class="error">* <?php echo $pay_PeriodErr;?></span>
        <br/>
        <p>Does your pay day result on the same days of the week:<br/>
        (i.e.: Every pay checks are paid on Fridays.)</p>
        <input type="radio" name="payDay" value="1" <?php if (isset($_POST['payDay']) && $_POST['payDay']=='yes') echo ' checked="checked"';?> />Yes
        <input type="radio" name="payDay" value="2" <?php if (isset($_POST['payDay']) && $_POST['payDay']=='no') echo ' checked="checked"';?> />No
        <span class="error">* <?php echo $payDayErr;?></span>
        <br/>
        <br/>
        <button onclick="myFunction()">Submit</button>
        <br/>
        <br/>
    </form>

2 个答案:

答案 0 :(得分:1)

在您的PHP代码中,当您需要使用收音机$_POST['pay_Period']=='bi_Weekly'字段中指定的值时,您正在检查value。您将这些设置为12,因此您需要使用这些。那将是提交给你的。见下面的代码

<form method="post" action="">
   <p><span class="error">* required field.</span></p>
   <p>What is the pay period reported by Company X:</p>
   <input type="radio" name="payPeriod" value="1" <?php if (isset($_POST['payPeriod']) && $_POST['payPeriod']=='1') echo ' checked="checked"';?> />Bi-Weekly
   <input type="radio" name="payPeriod" value="2" <?php if (isset($_POST['payPeriod']) && $_POST['payPeriod']=='2') echo ' checked="checked"';?> />Semi-Monthly
   <span class="error">* <?php echo $payPeriodErr;?></span>
   <br/>
   <p>What is the clients response to pay period:<br/>
      (How often do you get paid, once every two weeks or twice a month?)
   </p>
   <input type="radio" name="pay_Period" value="1" <?php if (isset($_POST['pay_Period']) && $_POST['pay_Period']=='1') echo ' checked="checked"';?> />Every two weeks
   <input type="radio" name="pay_Period" value="2" <?php if (isset($_POST['pay_Period']) && $_POST['pay_Period']=='2') echo ' checked="checked"';?> />Twice a month
   <span class="error">* <?php echo $pay_PeriodErr;?></span>
   <br/>
   <p>Does your pay day result on the same days of the week:<br/>
      (i.e.: Every pay checks are paid on Fridays.)
   </p>
   <input type="radio" name="payDay" value="1" <?php if (isset($_POST['payDay']) && $_POST['payDay']=='1') echo ' checked="checked"';?> />Yes
   <input type="radio" name="payDay" value="2" <?php if (isset($_POST['payDay']) && $_POST['payDay']=='2') echo ' checked="checked"';?> />No
   <span class="error">* <?php echo $payDayErr;?></span>
   <br/>
   <br/>
   <button onclick="myFunction()">Submit</button>
   <br/>
   <br/>
</form>

答案 1 :(得分:0)

您需要了解的有关单选按钮的所有内容均为here。 也许你会找到一些其他有用的信息

如果向下滚动一下,您将看到如何检查某些单选按钮。

古德勒克

EDIT:

错过了javascript,我会为你找一个解决方案 一段时间以前就已经提出了一个相关的问题,可以找到here