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>
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>
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
Related
In Cakephp3.2 I cant place spaces between radio buttons. I am sure there is an easy way.
Here is an example of what I am talking about from my test server, check the radio button area in Grammar section about 2/3rds down page.
http://andrewt.com.au/crm/students/studentassessment-eng7/35730
My code
<?php $opt1q1=' I wanted to watch TV, but the electricity went out unexpectedly.';
?>
<br>
<h3><?php echo 'Task 3: Grammar' ?> </h3>
<div class='alert alert-info'><?php echo '1 )'.$opt1q1; ?> </div>
<?php echo $this->Form->radio( 'a16', ['text'=>'a) noun', 'b) verb ','c) preposition ', 'd) pronoun ' ],
['default' =>$assessment['a16'] ,'style' => 'padding-left:5px;' ] );
The docs didnt help either and its maybe because I have an older version cakephp?
https://book.cakephp.org/3/en/views/helpers/form.html#creating-radio-buttons
Didnt work
space between the radio button and label Cakephp
i think what you are looking for are link breaks?
How to add a line break within echo in PHP?
https://www.w3schools.com/php/func_string_nl2br.asp
<input type="hidden" name="a16" value="" class="form-control"><label for="a16-0">
<input type="radio" name="a16" value="0" <?php echo $assessment['a16']==0 ?' checked=checked':'' ?> >a) noun</label><label for="a16-0">
<input type="radio" name="a16" value="1" <?php echo $assessment['a16']==1 ?' checked=checked':'' ?> >b) verb</label><label for="a16-1">
<input type="radio" name="a16" value="2" <?php echo $assessment['a16']==2 ?' checked=checked':'' ?> >c)preposition</label><label for="a16-2">
<input type="radio" name="a16" value="3" <?php echo $assessment['a16']==3 ?' checked=checked':'' ?> >d)pronoun </label><label for="a16-3">
<style>
label{
margin-left: 16px;
}
</style>
I'm making a website where users can insert data into a database and keep track of their medicine intake.
I have a simple form that submits to an SQL database:
<form method='post'>
<b>Dato:</b> <input type="date" name='date' id='date' value="<?php echo date('Y-m-d'); ?>" /><br /><br \>
<b>Tid:</b> <input type="time" name='time' id='time' value="<?php echo " $hour:$minute "; ?>" /><br /><br \>
<b>Medicin:</b><br /> <input type="radio" name="medicin" value="Medicin1" checked> Medicin1 <br />
<input type="radio" name="medicin" value="medicin2"> Medicin2 <br />
<input type="radio" name="medicin" value="other"> Other <input type="text" name="otherMedicin"><br /><br />
<b>Kommentar:</b><br /><textarea name='comment' id='comment'></textarea><br /> <hr \>
<input type='submit' value='Send' />
</form>
To submit it to the database I found a previous question about the same thing that I follow. Unfortunately I just can't get it to work!
html radio button with an "other" selection that has a text box
Here is the important part of my code:
$users_date = mysqli_real_escape_string($con, $_POST['date']);
$users_time = mysqli_real_escape_string($con, $_POST['time']);
$users_medicin = mysqli_real_escape_string($con, $_POST['medicin']);
$otherMedicin = mysqli_real_escape_string($db, $_POST['otherMedicin']);
if ($users_medicin == 'otherMedicin') {
$medicinField = $otherMedicin;
} else {
$medicinField = $users_medicin;
}
$users_comment = mysqli_real_escape_string($con, $_POST['comment']);
$query = "
INSERT INTO `myDatabase`.`medi_list` (`id`, `dato`, `tid`, `medicin`, `kommentar`) VALUES (NULL, '$users_date',
'$users_time', '$medicinField', '$users_comment');";
I'm still quite new to SQL so I'd appreciate any help you can throw my way :)
I see some basic problems in your code:
When you are populating the field "$otherMedicin", you are using $db instead of $con like the other statements.
$_POST[medicin] is NEVER going to be "otherMedicin", because that is the name of your text input field, not the radio button name. You should have this as your conditional:
if ($users_medicin == 'other') {
That should help a bit.
I am trying to edit a plugin field to use a drop down Expiry but need little bit of help. Here is what I have so far:
function payment_fields()
{
if ( $this->description )
echo wpautop(wptexturize($this->description));
echo '<label style="margin-right:46px; line-height:40px;">Credit Card Number:</label> <input type="text" name="aim_credircard" /><br/>';
echo '<label style="margin-right:30px; line-height:40px;">Credit Card Expiry (MM/YY) :</label> <input type="text" style="width:50px;" name="aim_ccexpdate" maxlength="4" /><br/>';
echo '<label style="margin-right:89px; line-height:40px;">CVV :</label> <input type="text" name="aim_ccvnumber" maxlength=4 style="width:40px;" /><br/>';
}
I want to be able to edit the following code to reflect a drop down month and year without creating a problem for the submission the front end and SELECT and SUBMIT the way it is right now.
echo '<label style="margin-right:30px; line-height:40px;">Credit Card Expiry (MM/YY) :</label> <input type="text" style="width:50px;" name="aim_ccexpdate" maxlength="4" /><br/>';
Thank you
I have two set of radio buttons in html form button and button1. I am using below code to
1.keep the default value checked (question1 for first set and answer2 for next set)
2.keep user radio button selection after the form submit
<div id="button_set1">
<input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(isset($_POST['button']) && $_POST['button'] == 'Yes') echo ' checked="checked"';?> checked /><label>question1</label>
<input onClick="show_list_lunid();" type="radio" name="button" value="No" <?php if(isset($_POST['button']) && $_POST['button'] == 'No') echo ' checked="checked"';?> /><label>answer1</label>
</div>
<div id="button_set2">
<input onClick="os_hpux();" type="radio" name="button1" value="Yes" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'Yes') echo ' checked="checked"';?> /><label>question2</label>
<input onClick="os_others();" type="radio" name="button1" value="No" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'No') echo ' checked="checked"';?> checked /><label>answer2</label>
</div>
Here if i use below code, the second radio button button1 is not sticking on to the user selection after form submit, it changing back to its default checked state.ie answer2. But the first set of radio buttons work fine.
If I remove the default checked option from the code, both radio buttons working fine after form submit. How can I keep the radio button checked after form submit while using checked default option for radios
So, the problem is you're setting the checked value twice upon form submission, resulting in selecting either the default value (from initial form state) or the value that has been submitted.
For this to work correctly, you'd need always use PHP to append the checked value to your radio elements, like this:
<div id="button_set1">
<input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(!isset($_POST['button']) || (isset($_POST['button']) && $_POST['button'] == 'Yes')) echo ' checked="checked"'?> /><label>question1</label>
<input onClick="show_list_lunid();" type="radio" name="button" value="No" <?php if(isset($_POST['button']) && $_POST['button'] == 'No') echo ' checked="checked"';?> /><label>answer1</label>
</div>
<div id="button_set2">
<input onClick="os_hpux();" type="radio" name="button1" value="Yes" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'Yes') echo ' checked="checked"';?> /><label>question2</label>
<input onClick="os_others();" type="radio" name="button1" value="No" <?php if(!isset($_POST['button1']) || (isset($_POST['button1']) && $_POST['button1'] == 'No')) echo ' checked="checked"';?> /><label>answer2</label>
</div>
Here's a working preview: http://codepad.viper-7.com/rbInpX
Also, please note that you're using inline JavaScript notation which is normally discouraged to keep dynamic JS content separate and more manageable ;-)
I had a similar problem recently, but had a lot more radio buttons to deal with, so I thought I'd abstract away the value checking functionality into a method that also creates the radio button itself, therefore minimising repetitive value-checking code. I've reworked mine to suit your variable names:
function createRadioOption($name, $value, $onClickMethodName, $labelText) {
$checked = '';
if ((isset($_POST[$name]) && $_POST[$name] == $value)) {
$checked = ' checked="checked"';
}
echo('<input onClick="'. $onClickMethodName .'();" type="radio" name="'. $name .'" value="'. $value .'"'. $checked .' /><label>'. $labelText .'</label>');
}
<div id="button_set1">
<?php createRadioOption('button', 'Yes', 'show_seq_lunid', 'question1'); ?>
<?php createRadioOption('button', 'No', 'show_list_lunid', 'question1'); ?>
</div>
<div id="button_set2">
<?php createRadioOption('button1', 'Yes', 'os_hpux', 'question2'); ?>
<?php createRadioOption('button1', 'No', 'os_others', 'question2'); ?>
</div>
This seems like a simple problem, but how do I create a basic HTML form that has a series of radio button options, with the last one being a text field to fill in a custom response (i.e. "Other").
What I have now:
Reason given for stop? <br>
<input type="radio" name="reason" value="Fit Description">Fit Description<br>
<input type="radio" name="reason" value="Suspicious Behavior">Suspicious Behavior<br>
<input type="radio" name="reason" value="No Reason Given">No Reason Given<br>
<input type="radio" name="reason" value="">Other<br>
Just add a text input field to it.
Reason given for stop? <br>
<input type="radio" name="reason" value="Fit Description">Fit Description<br>
<input type="radio" name="reason" value="Suspicious Behavior">Suspicious Behavior<br>
<input type="radio" name="reason" value="No Reason Given">No Reason Given<br>
<input type="radio" name="reason" value="">Other <input type="text" name="other_reason" />
jsFiddle example
Create a text field, and set it to display:none;
Then with jQuery, detect when the 'Other' radio button is checked and show the textbox.
Then on your process script, do and if statement to see if the value of your radio button group is "" (nothing), and grab the post data of the textbox and do what you want with it.
There is a neat way to add text field alongside radio buttons without using functions or Javascript (jQuery).
Just add the radio btn (Other) and the text field next to it, on the top of your HTML form. Here is what i use:
Color:
<input type="radio" name="title[<?=$red?>][color]" value="" <?php if ($row['color'] != ' ') {echo 'checked';} ?> />Other <input type="text" name="title[<?=$red?>][color]" value="<?php echo $row['color'] ?>" style="width: 200px;" /> |
<input type="radio" name="title[<?=$red?>][color]" value="natural" <?php if ($row['color'] == 'natural') {echo 'checked';} ?> />natural|
<input type="radio" name="title[<?=$red?>][color]" value="stain" <?php if ($row['color'] == 'stain') {echo 'checked';} ?> />stain |
<input type="radio" name="title[<?=$red?>][color]" value="white" <?php if ($row['color'] == 'white') {echo 'checked';} ?> />white|
Basically you do not put value on the "Other" radio input, but on the text input so whatever you write in the text field will be send to db - its 1st field in the FORM. If nothing in the text field - the other checked radio input will be processed.
Hope this helps.
Simply add a text field as you would normally but give it the same name attribute as the others, that way when accessing them you'll get one of them.
In JavaScript (which I assume you'll be using?) just access these elements and check the the textfield is empty, if it is get the radio buttons.