Reputation: 3
I'm new to coding and have a questionnaire that requires a selection of two choice... I can chose the selection and have it output the correct response, however, the choice chosen does not retain. How can i make it stay after a user hits the submit button? I've tried to research it and used several different format but it was still not working. Not sure if it's my syntax or I'm just do not have the right coding. I've provided a copy of the code.
<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>
Upvotes: 0
Views: 3619
Reputation: 573
Everything you need to know about radio buttons is right here. Maybe you will find some other useful information
if you scroll a bit down you will see how you can keep certain radio buttons checked.
goodluck
EDIT:
Missed the javascript, Ill look for a solution for you A related question was already asked some time ago, it can be found here
Upvotes: 0
Reputation: 4538
In your PHP code, you're checking for $_POST['pay_Period']=='bi_Weekly'
when you need to be using the values specified in your radio value
field. You have these set to 1
and 2
, so you need to use those. That is what will be submitted back to you. See code below
<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>
Upvotes: 1