I am a php programmer .I have an image rating page ,in which it displays the image gallary with radio buttons once selected and submitted ,the image button values go to the table .when I retrieve the images they are rendered properly , however they do not show the previous radio button rating selection.
My question is how to retain the most recent radio selections , when images are fetched into the browser.
I'm not sure what your options are but it would be something like this;
<input type="radio" name="image_option" value="1"<?php $_POST['image_option'] == 1 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="2"<?php $_POST['image_option'] == 2 ? ' checked="checked"'; ?> />
<input type="radio" name="image_option" value="3"<?php $_POST['image_option'] == 3 ? ' checked="checked"'; ?> />
Hope that helps.
You'll have to set the "checked" attribute for the appropriate radio buttons that you want to be selected.
if you want to default select then use "Checked".
and if you want to checked on resent selective button. then on submit time, store radio button value in session and when browser get request after submission radio value then retrieve session value and apply checked on previous select radio button.
Your problem is that you need:
The value in the database to match
The value of the radio button itself
Which will allow you to:
Compare the database value (DBV) with the radio button value (RBV) and set it as checked if the comparison comes back as true.
The variables below are:
$ar_rvbs = the array of radioset values (strings or booleans, usually) you are going to loop through and check against the stored DBV.
$value = the value of each item in the radioset value array
$attrval = the stored value for radio button. it doesn't necessarily have to be in a database. you could use the post method to pass it from one page to the next.
$checked = if the DBV matches the RBV when the loop goes through, this will be set to the string "checked" otherwise it is just an empty string.
function makeRadioSet($ar_rvbs,$attrval=[DBV] /*A*/
{
foreach ($ar_rvbs as $value)
{
$checked = ''; /*B*/
if ($attrval==$value) /*C*/
$checked = "checked"; /*D*/
echo '<input type="radio" name = "fieldname" value = "'.$value.'" '.$checked.'>';
}
}
/A/ Pass the list of RBVs as an array and the DBV as a variable
/B/ Set checked to an empty string because that will be the default for all the radio buttons in the set except the one that matches the DBV
/C/ Compare the DBV to the current RBV being processed by the loop from the RBV array
/D/ If the comparison from step C returns true, make the checked string available for insertion into the input element
The echo takes care of generating each radio set option and making sure the one that matches the DBV has "checked" in the input element tag
You can use in following method
#{
string MaleChecked = "";
string FemaleChecked = "";
if(#Model.Gender=="Male")
{
MaleChecked = "Checked";
}
else
{
FemaleChecked = "Checked";
}
}
if form use
Male<input type="radio" name="Gender" id="rdoGender" value="Male" #MaleChecked>
Female<input type="radio" name="Gender" id="rdoGender1" value="Female" #FemaleChecked>
after reload the page you have to add the attribute checked to the radio buttons you have to check.
The correct syntax is:
<input type="radio" name="foo" id="bar" value="1" checked="checked" />
Related
I am building a site with a number of independant check boxes that collect information about industry topics. I user will check the ones of interest and store “Yes” in the database which is Filemaker.
When they return, they will see the ones previously checked and can uncheck if needed.
To this end, I am trying to get a check box to display as checked if the database value is equal to “Yes” and display as unchecked if the value is blank. Also, of the user checks the checkbox on the form, it will send the value of “Yes” back to the database and a value of blank if the check box is unchecked.
So far, I am only able to display the “Yes” or blank for fields. Here is my code so far:
<input type="text" name="Core_Compentencies__Marketing" value="<?php echo $port_row->getField('Core_Compentencies::Marketing'); ?>"></td>
Any help is appreciated.
Thanks.
usually i use helper function to decide whether field value is checked or not.
<?php
function isChecked($value = '', $defaultVal = 'Yes')
{
if($value == $defaultVal)
{
return 'checked';
}
}
?>
<input name="checkbox" type="checkbox" value="Yes" <?php echo isChecked(Core_Compentencies::Marketing); ?>>
I have created a table of checkboxes. The rows are divided up by category, and either a checked or unchecked checkbox gets displayed under a department column. I have a lot of code so I will break down what I am supplying. I am creating an array via each column (odd method, yes). I have noticed that if all check boxes are deselected, it will return the hidden value of 0 each time it loops. Thats great, thats what I wanted. However, if the box is selected, it returns both the value of 0 and the value of 3. For instance:
Test = Array()
Test[0] => 0
Test[1] => 3
How can I prevent it from posting the hidden value?
$row_two = mysql_query("SELECT dept_id FROM categories WHERE cat_name = '{$cats['cat_name']}' and bus_id = '{$busUnits['bus_id']}'");
while (($test_two=mysql_fetch_assoc($row_two)))
{
$AnotherTest = implode(',', $test_two);
$WhatTest = explode(",", $AnotherTest);
if(in_array("3",$WhatTest, TRUE))
{
echo '<input type="hidden" name="Cat_CBC_Test_One[]" value="0">';
echo '<td><input type="checkbox" name="Cat_CBC_Test_One[]" value="3" checked></td>';
}
else
{
echo '<input type="hidden" name="Cat_CBC_Test_One[]" value="0">';
echo '<td><input type="checkbox" name="Cat_CBC_Test_One[]" value="3"></td>';
}
To detect unchecked Checkboxes i would add a hidden field with a different name, like _Cat_CBC_Test_One. That way you dont have the issue with the hidden field interfering.
Then, on the server, you scan through for parameters that start with _ and add the missing "false" Parameters for further processing.
I have a form with checkboxes that users fill out and submit. I then have another form that allows them to edit the information they submitted. This second form pre-populates with the information they submitted on the first form. This is fine with text fields, but how do I pre-populate the checkboxes? I.e. if they checked a checkbox on the first form, how do I get the second form to recognise that and display a checked checkbox?
I'm new to Php so sorry I can't be more technical with this query!
Thanks
Luke
When you submit the form, you have the data on the next page. So if theres a checkbox looking like this:
<input type="checkbox" name="over18" value="1" />
You have to check if the person selected the "over 18"-field.
if ($_POST['over18'] == '1') {
$checked = 'checked="checked"';
}
else {
$checked = ''; //If it's not checked
}
Then your output for the checkbox looks like this:
echo '<input type="checkbox" name="over18" value="1" ' . $is_checked .'/>';
So everytime the Checkbox is checked, the next page checks the Checkbox too.
CodeIgniter + Grocery Crud - how to set fields to either on/off (bool, checkbox) and also how to set it to be a select dropdown of numbers (1-10) ?
I have grocerycrud set up, and on some fields they are either:
bool values (but set as ints or varchars so groceycrud shows it as a
or
select dropdown with values 0-10 (these are stored as ints, but it is for a rating system)
any ideas?
thanks
For this you can check field_type method. In your case you need:
field type true_false for on/off boolean. For example:
$crud->field_type('my_field','true_false');
The default on/off for grocery CRUD is active/inactive (1/0) but you can change the text from the lang file at: assets/grocery_crud/languages/english.php (or your language) to on off.
For the second scenario of dropdown list you can use the field_type with type "dropdown" or "enum". For more you can go to: http://www.grocerycrud.com/documentation/options_functions/field_type#dropdown-field and http://www.grocerycrud.com/documentation/options_functions/field_type#enum-field that also have examples.
Also consider that the dropdown type is available only for versions >= 1.3.2
You can change the values of radio buttons in the config file for your language. for english, it's here:
assets\grocery_crud\languages\english.php
Just change these 2 lines:
$lang['form_inactive'] = 'inactive';
$lang['form_active'] = 'active';
To whatever you want, in your case, something like:
$lang['form_inactive'] = 'female';
$lang['form_active'] = 'male';
and make sure to change the field to boolean, if you haven't yet:
$this->grocery_crud->change_field_type('field_name','true_false');
// now This part is for update or edit
$crud->callback_edit_field('government',array($this,'radio_edit_callback'));
//Function for the callback_edit_field
public function radio_edit_callback($value) {
//$value will be having the present value(Y or N) that is in the list or database.
if($value == 'Y') {
return '<input type="radio" name="government" value=" '.$value.' " checked="checked" /> Yes
<input type="radio" name="government" value="N" /> No ';
} else {
return '<input type="radio" name="government" value="Y" /> Yes
<input type="radio" name="government" value=" '.$value.' " checked="checked" /> No';
}
}
how can i process all radio buttons from the page?
<input type="radio" name="radio_1" value="some" />
<input type="radio" name="radio_1" value="some other" />
<input type="radio" name="radio_2" value="some" />
<input type="radio" name="radio_2" value="some other" />
this buttons will be added dynamically so i will not know the radio buttons name(and also the number of the buttons). Is there a way to get all radio values, with a for loop or something like this? thanks
Use a foreach loop
<?php
foreach ( $_POST as $key => $val )
echo "$key -> $val\n";
?>
$key will be the name of the selected option and $val, well, the value.
Since the browser will just change all your input to HTTP-formatted form data, you won't be able to tell what data is from a radio button versus a text box or other input.
If the naming convention is the same as your example, just loop until you don't find a value:
<?
for ($idx = 1; $idx <= 1000; $idx++) {
if (isset($_REQUEST["radio_$idx"])) {
// handle value
}
}
?>
EDIT Alternatively, if your form is generated dynamically, you could write the number of radio buttons it created as a hidden field in the form.
If you are able to alter the form that is being generated, you could write a hidden input that provided a list of all the radio buttons that you want to look for. As you are writing the radio buttons, just make a semi-colon-separated list of all the names that you make. When you are done, write that to a hidden input. Something like this:
On the source form:
<input type="hidden" name="radio_button_list" value="some_value;other_value" />
Then in your handler:
<?
$list = explode(';', $_REQUEST['radio_button_list']);
foreach ($list as $name) {
$value = $_REQUEST[$name];
// handle name & value
}
?>
jheddings' example says it all. However, you will never get the names / values of all buttons - just the selected one from each group. If you need literally all values, you will have to use Javascript.