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?
Related
Good day!
I'm doing a site. There is a checkbox which update the option I choose in phpmyadmin.
But if I press submit, then f5/reload page, the preference I choose disappears on client/html side, (look the image for better understanding)
Image, open me!
So basically I want the site remember my choice in the "user account"
I tried with
<form action="pr.php" method="POST">
<h3 style="color:red;">Numbers?</h3>
<input type="checkbox" name="a1" value="1" <?php if(isset($_POST['a1'])) echo "checked='checked'"; ?> /><label>One</label><br/>
<input type="checkbox" name="a2" value="2" <?php if(isset($_POST['a2'])) echo "checked='checked'"; ?> /><label>Two</label><br/>
<input type="checkbox" name="a3" value="3" <?php if(isset($_POST['a3'])) echo "checked='checked'"; ?> /><label>3</label><br/>
<input type="checkbox" name="a4" value="4" <?php if(!is_null($_POST['a4'])) echo "checked='checked'"; ?> /><label>Four</label><br/>
<input type="submit" value="submit" name="submit">
It doesnt work with isset, empty or is_null.
Thanks for all!
Unless you save the information on a database, even if you clicked on submit, after you refresh the webpage you are going to loose the the values of the variables.
You could use cookies to save the value of the choice that was checked:
setcookie("checkbox1","checked");
First parameter is name of the cookie, second one is the value of the cookie, and you can add a third one with the time you want the cookie to have before it expires.
And in your if condition, you could do this:
<?php if($_COOKIE['a1'] == "checked") echo "checked='checked'"; >
For more info, check this link.
so I'm creating a small form where you can update the values of some fields pulled from a database.
Two of the fields I have however are acting wrong, when the page loads they display the information correctly but when you press 'submit' the fields in the MySQL table go blank. All the other fields update correctly including a checkbox, but not these two radio buttons.
Here's the code for it, I'm sorry to be asking this but does anyone see the error? I'm rather new with PHP so I'm not sure what to look for.
Snippet of the PHP Code (I can post the full code of it, but it's long):
<?php
if(isset($_POST['submit'])) {
$mbr=$_POST['mbr'];
$rec=$_POST['rec'];
$update = $dbconnect->query("UPDATE testing SET mbr='$mbr', rec='$rec'");
}
?>
HTML Code for the 2 radio buttons:
<input type="radio" name="mbr" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" <?php echo $noChecked; ?>>No
Nevermind I'm dumb and tired and missed the obvious solution, I forgot to put the values of the buttons in their code.
How they should be
<input type="radio" name="mbr" value="up" <?php echo $upChecked; ?>>Up <input type="radio" name="mbr" value="down" <?php echo $downChecked; ?>>Down
<input type="radio" name="rec" value="yes" <?php echo $yesChecked; ?>>Yes <input type="radio" name="rec" value="no" <?php echo $noChecked; ?>>
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
I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.
I'm working with Joomla User Form. I need to create some extra fields. I googled, and got this tutorial
I followed but that wasn't enough for me because I have to handle with checkbox and radio button. The following form code didn't save submission data (I mean checked/uncheked).
<input class="inputbox" type="checkbox" name="hasweb" id="hasweb" size="40" value="<?php echo $this->user->get('hasweb');?>" />
But the following is fine when I put any data there.
<input class="inputbox" type="text" name="hasweb" id="hasweb" size="40" value="<?php echo $this->user->get('hasweb');?>" />
I'm novice in Joomla. Please help me.
There is a difference between the value and the checked state with HTML checkbox elements. So if you want to have the checkbox represent the actual choice you have to do something like this:
<input class="inputbox" type="checkbox" name="hasweb" id="hasweb" size="40" value="reallyhasweb" <?php echo $this->user->get('hasweb') == "reallyhasweb" ? 'checked="checked"' : ''; ?>" />
This should place checked="checked" in the HTML when the user has selected the checkbox and will make the checkbox selected.