I'm having a problem with CodeIgniter repopulating a form after validation fails.
HTML:
<label for="public">Anyone</label>
<input type="radio" name="target" value="public" <?php echo set_radio('target', 'public', TRUE); ?> />
<label for="direct">Specific</label>
<input type="radio" name="target" value="direct" <?php echo set_radio('target', 'direct'); ?> />
When I first load the form I get this source code:
<label for="public">Anyone</label>
<input type="radio" name="target" value="public" checked="checked" />
<label for="direct">Specific</label>
<input type="radio" name="target" value="direct" />
... so the third parameter is working (the default "TRUE")
But when I submit the form with validation errors on other fields, the form is reloaded with no radio button selected.
A var_dump($_POST) after submitting (with intentional validation errors on other fields) shows this:
array (size=9)
'target' => string 'direct' (length=6)
...
but no radio button is selected.
Got to be something simple... help?
The answer is apparently to add a fake rule to the radio button.
$this->form_validation->set_rules("tenderType", "", "trim");
Thanks to #FuzzyTree pointing to the solution here: stackoverflow.com/q/16473459/3574819
Related
So, I have a questions regarding the input type radio in HTML. As you know, you can put checked as a value, this will mark it as checked.
The story is I am getting a 0 or 1 value from my database. I am then checking if it's 0 or 1 and will then mark one of the radio button's as checked.
My code is as follows:
<?php if($pay_op == 0) { ?>
<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } elseif ($pay_op == 1) { ?>
<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>
My problem now is, whenever I try to mark the other radio button as checked by clicking on it, both radio buttons are checked?
I thought that this might have something to do with me checking if the value returned from the database is 0 or 1 and it will keep one of the radio buttons checked until that value is changed. Now my question is, does anyone know a solution to this issue so that whenever someone clicks on something different than the default checked radio button it will actually check that one and not both of them?
Any tips are highly appreciated! =)
Thanks!
Radio buttons work basically as a named group. The browser only un-checks a radio button if it is linked to the other radio buttons with a property called name.
<?php
if($pay_op == 0)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
elseif($pay_op == 1)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
?>
Im using Codeigniter - I am displaying radio buttons like so :
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime"> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime"> Part Time
</fieldset>
Which works fine, they display.
Here is the code in the model.
'time'=>$this->input->post('time'),
which also works fine , it saves the choice to the database. But how do I get the radio button to populate when the page loads with the choice from the database?
You can use the form helper, which has a function called "set_radio", the very last function on this page;
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
<input type="radio" name="time" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?> />
<input type="radio" name="time" value="parttime" <?php echo set_radio('time', 'parttime'); ?> />
I hope this helps.
Just as #Craig said, using set_radio is what you will need.
php
<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />
Note
The value of this radio button is "2". Whatever the value is, needs to be the second parameter in set_radio.
Your code would look something like this:
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?>> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime" <?php echo set_radio('time', 'parttime'); ?>> Part Time
</fieldset>
If you want either option checked by default (when the page loads) that is where you would add the third parameter TRUE to the corresponding radio.
ALSO NOTE
In your controller, you will need to load the form_validation library for any of this to work.
php
$this->load->library('form_validation');
Hope this helps!
EDIT
My apologies, I misread the question. What I had above is...before the form is officially/successfully submitted. In your case, the form has already been (successfully) submitted and you want to...edit the form (for lack of better words) - so you need a way of populating the inputs with whatever the user chose.
What I have done in the past - in my view - is just have a ternary operation right on the element.
For example, let's say I am returning a user, and there is a "time" property.
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" value="fulltime" <? echo($user['time'] === 'fulltime') ? selected="selected" : ''; ?> />
<input type="radio" name="time" value="parttime" <? echo($user['time'] === 'parttime') ? selected="selected" : ''; ?> />
</fieldset>
Maybe that's closer to what you are looking for?
Hello I am a beginner in PHP
I have created a form and there are few radio button with YES/NO options... every thing is working fine but if i submit form without clicking radio button options, it shows the following error... I know there is some code to be written but not getting exactly
'Notice: Undefined index: ws_id in D:\xampp\htdocs\mc\db_def.php on line 18'
Solution in your php use isset() to check
Use required in input tag "very basic validation"
<label for="input1">1:</label><input type="radio" name="test" id="input1" required value="1" /><br />
<label for="input2">2:</label><input type="radio" name="test" id="input2" value="2" /><br />
<label for="input3">3:</label><input type="radio" name="test" id="input3" value="3" /><br />
Check this fiddle http://jsfiddle.net/Pw5vQ/
Solution 1:
If you won't check radio button or checkbox the form won't post it so you will not find these inputs on the $_POST superglobal variable.
Try to var_dump($_POST); with checked and unchecked version.
Just check the required form name with isset() like this isset($_POST['ws_id']).
If isset gives you false it means it won't checked.
Solution 2:
You can preset the inputs with html attribute checked="checked".
For example:
You would like to check the NO radio button for default:
<input type="radio" name="ws_id" value="YES"/>Yes
<input type="radio" name="ws_id" value="NO" checked="checked"/>No
I guess you are getting this error when first accessing the page, so there is no sent data, so your $_POST['ws_id'] or $_GET['ws_id'] or $_REQUEST['ws_id'] has no value in it as there is no data sent.
Use
if (isset($_POST['id'])) // do what you were doing on line 18
//or
if (isset($_GET['id'])) // do what you were doing on line 18
//or
if (isset($_REQUEST['id'])) // do what you were doing on line 18
depending on your choice.
Need help to passing label text to another page via session array and also i have tried to post radio button values but problem is that radio button values didn't display into another page?
Index Page Php Function
<?php
session_start();
if (isset($_REQUEST['demo'])) {
$_SESSION['size'] = array('Small', 'Medium');
}
?>
$(document).ready(function () {
$('.form-radios label').attr('checked', 'unchecked');
$(".form-radios label").click(function(){
$(this).attr('checked', 'checked').addClass('checked');
$('label').not(this).removeClass('checked');
});
});
</script>
Index Page Form
<form action="page2.php" method="post">
<div class="form-radios">
<input type="radio" name="size" id="size" value="Small" />
<label for="size">Small</label>
<input type="radio" name="size" id="size" value="medium"/>
<label for="size">Medium</label>
<input type="radio" name="size" id="size" value="large"/>
<label for="size">Large</label>
<input type="radio" name="size" id="size" value="xl"/>
<label for="size">Xl</label>
</div><br />
<input type="submit" name="demo" value="submit" />
</form>
Page2 Function
<?php
session_start();
echo $sizes=$_SESSION['size'];
?>
<input type="text" value="<?php echo $sizes ?>" />
Your session is an array you have to loop it.
foreach ($_SESSION['sizes'] as $size)
{
echo $size;
}
But I want to post radio values how could I send?
Radio values from your form are sent automatically to the action specified when a user press the submit button. In the action page they can retrieve them via the $_POST or $_GET global array. In your case, since you are using the POST method, you can retrieve your radio box via:
$_POST['size'];
Actually I want to send when someone click on label then label values to another page.
Well, you can do that by make Javascript trigger an event when a radio box is selected and send a request via AJAX.
This question is similar to my previous question but not the same ... please check out....I am using totaly 3 webpages; form elements are distributed among two pages, "eg1.html" and "eg2.html", but all the form elements should be submitted to "eg.php".
Here is the code for eg.php which accepts the form elements from both eg1.html and eg2.html:
$size=$_POST["fontsize"];
$label=$_POST["label"];
$age=$_POST["age"];
$sex =$_POST["sex"];
code for eg1.html
<html>
<body>
<form action="eg.php" method="post">
<input type="radio" name="fontsize" value="3"/>
click here to select other options which includes age and sex
<input type="radio" name="label" value="stacks"/>
<input type="submit" name = "down" value = "down">
</form>
</body>
Now What would be the code for eg2.html? just check out sample partial html code :but needs to be compleated....
<input type="radio" name="age" value="3"/>
<input type="radio" name="sex" value="female"/>
The code should work exactly like this:
First user will open eg1.php he selects only one option that is "fontsize" .. next he clicks on the "link to eg2.html" to select two more options "age" and "sex" after selecting... he will be redirected back to eg1.php where he has to select one more option that is "label" ... then he will submit the form to eg.php. Which will hold all form elements those are 'fontsize' 'age' 'sex' and 'label' .....
I have seen many website using this technique please check out cooltext.com where user will get an option to click on the font image which will redirect him to fonts page after selecting one of the fonts images he will be redirected back to homepage,where he can select some other form elements or form elements and finally submits the form .... i have also seen many websites using this technique , i think this can be done using JQUERY/JavaScript but not sure ...please help me to fix this problem guyz,.,,,
Using js you can have the entire form on one page and divide it in steps like this
<form action="eg.php" method="post">
<div class="step1">
<input type="radio" name="fontsize" value="3"/>
click here to select other options which includes age and sex
<input type="radio" name="label" value="stacks"/>
<input type="submit" name = "down" value = "down">
</div>
<div class="step2">
click here to go back to step1
<input type="radio" name="age" value="3"/>
<input type="radio" name="sex" value="female"/>
<input type="submit" value="Submit"/>
</div>
</form>
js:
$('#step1_submit').click(function(){
$('#step1').hide();
$('#step2').show();
});
$('#step2_back').click(function(){
$('#step1').show();
$('#step2').hide();
});