Assuming t开发者_开发问答his that we in time insert data in database has 3 input:radio
and with select one from they, insert value it to database. now how in select from database same radio that inserted in database, is checked.
Example:
We have 3 input:radio
as:
<input type="radio" name="type1" value="value1">
<input type="radio" name="type2" value="value2" checked>
<input type="radio" name="type3" value="value3">
With checked value2
inserted it in database, now we want show(select
) all radios and checked it radio that is inserted to database.as(this is after select from database):
<input type="radio" name="type1" value="value1">
<input type="radio" name="type2" value="value2" checked> // this value was in the database
<input type="radio" name="type3" value="value3">
How can fix it with PHP?
You should, as alreay said, use the same name for the radio buttons. Also, the right sintax is checked="checked"
:
<input type="radio" name="type" value="value1">Value 1
<input type="radio" name="type" value="value2" checked="checked">Value 2
<input type="radio" name="type" value="value3">Value 3
When retrieved from database, you can check if value equals to the one in your html, and set a checked attribute accordingly.
<input type="radio" name="type" value="value1" <?php echo ($query_hs->type == 'value1') ? 'checked="checked"' :'';?>>Value 1
<input type="radio" name="type" value="value2" <?php echo ($query_hs->type == 'value2') ? 'checked="checked"' :'';?>>Value 2
<input type="radio" name="type" value="value3" <?php echo ($query_hs->type == 'value3') ? 'checked="checked"' :'';?>>Value 3
Just substitute your own values with my standard one, like:
<input type="radio" name="type" value="hotel" <?php echo ($query_hs->type == 'hotel') ? 'checked="checked"' :'';?>>Hotel
First of all, name of the radio element should be same for all three radio buttons so that they form a group. Otherwise, your users will be able to select all three.
Second, define the set of values as array in PHP. And run a forloop to generate HTML code. Something on these lines -
<?php
$radio_values = array("value1", "value2", "value3"); // Assuming your set of values is static
foreach($radio_values as $value) {
$value_from_db = read_radio_value(); // Replace this with your logic to fetch values from database
if ($value_from_db == $value) $checked = "checked";
else $checked = "";
echo "<input type=\"radio\" name=\"type\" value=\"{$value[$i]}\" {$checked}>";
}
?>
精彩评论