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>
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>
Is it possible to have a single checkbox with two values ? i have a checkbox called "full-day ?".
<div class="input-field col s2">
<p>
<input type="checkbox" id="test6" value="yes" ng-model="isFull"/>
<label for="test6">Half Day</label>
</p>
</div>
if the user checks it a yes should be passed when i press submit button and if the checkbox is not checked a no have to be passed.As far as i know if the checkbox is not activated html treats as if checkbox is not present and it wont be included in the query string.Here using radio button is not an option.Please let me know how do i go about it.
This is simple:
<input type="hidden" id="test6" value="no" ng-model="isFull" checked>
<input type="checkbox" id="test6" value="yes" ng-model="isFull">
<label for="test6">Half Day</label>
It is boolean.
Code is processed sequentially
Only the last 'checked' answer is used.
Therefore put the 'non' answer first but hide it and leave it always checked. The positive answer comes second.
If the form is editable (user can come back and change their answer) code to check the single visible checkbox appropriately e.g.
<?php if ($fieldValue=='Yes' ){ echo 'checked'; } ?>
Works for me!
Not really.
You can fake things with JavaScript that injects hidden inputs, but this is better handled with server side code.
e.g.
my $value;
if ($request->param("checkbox_name") {
$value = "yes";
} else {
$value = "no";
}
I'm facing one problem in yii2 radio buttons while using with yii2 dynamic forms package. yii2 is not generating the radio button with id attribute. Due to missing of id attributes in radio buttons, yii2 dynamic forms radio buttons values are always setting as 1. So Please help me how to overcome this problem.
Edited
<?= $form->field($client_allow_acces, "[$i]access_type")->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'], ['uncheckValue' => null, 'id'=>'custom_id_value']) ?>
I'm getting the html output below
<div class="form-group field-clientallowaccess-0-access_type required">
<label class="control-label" for="custom_id_value">Access Type</label>
<input type="hidden" name="ClientAllowAccess[0][access_type]" value="">
<div id="custom_id_value">
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="1"> Allow access</label>
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="2"> Can't allow access</label>
</div>
<div class="help-block"></div>
</div>
My custom id value is coming in div element. but I need it in radio button itself.
Thanks in advance.
You need to use following code because of your given id is assigned to div in your code. that is on <div id="custom_id_value">.
<?= $form->field($client_allow_acces, '[$i]access_type')->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
$return = '<label class="modal-radio">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="custom_id_value_'.$index.'" >';
$return .= '<span> ' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
}]); ?>
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
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.