Assuming this 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}>";
}
?>
Related
All the inputs have the same name as option<?=$x?> for each loop but always the input with type=hidden of the last line overrides all of them although one of the radio buttons with same name option<?=$x?> is selected
Is there anyway so that I can check whether anyone out of four radios of above is chosen or not, if not then I have to pass the last input instead of them
<input type="radio" name="option<?=$x?>" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option<?=$x?>" value="null;<?=$data['id'].';'.$data['subject']?>">
You can try this
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option[<?=$x?>]['hidden']" value="null;<?=$data['id'].';'.$data['subject']?>">`
Now when you post you will get option array
$options = $_POST['option'];
$radioValue = $options[$x]['radio'];
$hiddenValue = $options[$x]['hidden'];
You can read value like this
$optionValue = isset($options[$x]['radio']) ? $options[$x]['radio'] : $options[$x]['hidden'];
Hope it may help you
I have a list of checkboxes and if checked their values are posted to json file which is working fine. When user uncheck all of them I get NULL value in array and if I set its value="" its returns depends :[""]. what I want is if user did not check any checkbox it returns depends:[] only neither NULL nor "".
Here is my code:
<label for="depends">Linked With</label></br>
<input type="checkbox" name="depends[]" value="1" id="ch2" > 1
<input type="checkbox" name="depends[]" value="2" id="ch3" > 2
<input type="checkbox" name="depends[]" value="3" id="ch5" > 3
<input type="checkbox" name="depends[]" value="4" id="ch6" > 4
<input type="checkbox" name="depends[]" value="5" id="ch7" > 5
<input type="checkbox" name="depends[]" value="6" id="ch8" > 6
<input type="checkbox" name="depends[]" value="7" id="ch9" > 7
in php code I am calling it as
'depends'=>$_POST['depends']
in your php code you can handle it like that :
<?php
/* if $_POST['depends'] is not empty then you store the value, else you create an empty array [] */
$var = isset($_POST['depends']) ? $_POST['depends'] : array(); /* you can replace $var */
print_r($var);
?>
I am trying to set up a voting process where there are 20 sets of two radio buttons you can select from. Then when a "vote" button is clicked I can collect the values of the selected buttons and add +1 to their respective database cells.
I am new to MySql and am struggling...
The database Table looks like..
ID Park Votes
1 Zion 0
2 Grand Canyon 0
3 Arches 0
4 Canyonlands 0
5 Yosemite 0
6 Yellowstone 0
The HTML form
<form action="/databases/save.php" method="post">
<input type="radio" name="match1" value="1" />Zion
<input type="radio" name="match1" value="1" />Grand Canyon
<input type="radio" name="match2" value="1" />Arches
<input type="radio" name="match2" value="1" />Canyonlands
<input type="radio" name="match3" value="1" />Yosemite
<input type="radio" name="match3" value="1" />Yellowstone
<input type="image" src="/graphics/logo.png" />
</form>
And the save.php file
<?php
if (isset($_POST['match1'])){
$match1 = $_POST['match1'];
mysql_query("INSERT INTO results () VALUES ('1')");
}
?>
I am getting thrown off by how to get the selected radio and apply a +1 to the correct table cell. Thanks in advance
Your HTML is incorrect for radio inputs. If you want the user to select only one park, all inputs must have the same name. Use the value attribute of the radio input to store the ID of the park (from the MySQL table):
<form action="/databases/save.php" method="post">
<input type="radio" name="match" value="1" />Zion
<input type="radio" name="match" value="2" />Grand Canyon
<input type="radio" name="match" value="3" />Arches
<input type="radio" name="match" value="4" />Canyonlands
<input type="radio" name="match" value="5" />Yosemite
<input type="radio" name="match" value="6" />Yellowstone
<input type="image" src="/graphics/logo.png" />
</form>
The selected value for that input will be returned as $_POST['match'] if one item was selected or else $_POST['match'] will not be set at all.
So on your save.php page you can update your table like this (assuming your table is named "parks_table"):
<?php
if (isset($_POST['match'])) {
mysql_query( 'UPDATE `parks_table` SET Votes = Votes + 1 WHERE ID = '.$_POST['match'] );
}
?>
Since you seem to be starting with PHP and MySQL please read: Why shouldn't I use mysql_* functions in PHP?
<button type="submit" name="vote" id="vote" value=" ' . $row['candidate_position'] . ' " class="btn btn-success">CAST VOTE</button>
<?php
$vote = $_POST['vote'];
if (isset($_POST['vote'])) {
mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE position_id='$vote'");
echo "string";
}
mysqli_close($con);
?>
I have a form such as the one below:
<input type="checkbox" name="type" value="wash"><label>Wash</label><br>
<input type="checkbox" name="type" value="no wash"><label>No Wash</label><br>
<label>Other (Specify)</label><br>
<input name="type"><br>
If you notice for all three i am using "type" as the input name. The point being that the user will be given two options, if none of the two options apply to them they should enter a value in other. Now in the database i have the field type, so if they selected the first two and entered a value in the field or if they only wrote a value in the field i still want it to to be part of the type field. So how can i make it so that if they select the input field it should also insert in "type".
Do you mean something like this?
HTML:
<input type="checkbox" name="type[]" value="wash"/><label>Wash</label>
<input type="checkbox" name="type[]" value="no_wash"/><label>No wash</label>
Other type:
<input type="text" name="other_type"/>
PHP:
if (!empty($_REQUEST['other_type']))
$_REQUEST['type'][] = $_REQUEST['other_type'];
var_dump($_REQUEST['type']);
First of all you should better use radio buttons instead of checkboxes.
Then you could do the following
<input type="radio" name="type" value="wash"/><label>Wash</label>
<input type="radio" name="type" value="no_wash"/><label>No wash</label>
<input type="radio" name="type" value="other"/><label>Other</label>
<input type="text" name="other_type"/>
Your PHP would then look like this:
if ($_REQUEST["type"] == "wash"){
echo "Wash me please";
}else if ($_REQUEST["type"] == "no_wash"){
echo "no wash";
}else if ($_REQUEST["type"] == "other"){
echo "you want to ".$_REQUEST["other_type"];
}
If you use JS you could even disable the textbox unless the user selects the third option.
Edit: If I got your comment right it would be the easiest like this:
<input type="checkbox" name="wash_me"/><label>Wash your car?</label>
<input type="text" name="other"/><label>What else can we do for you?</label>
PHP
if (isset($_REQUEST["wash_me"]){
echo "wash my car please";
}
if (strlen($_REQUEST["other"]) != 0){
echo "and do the following: ".$_REQUEST["other"];
}
I have a radio button array that I need help with. Here is the code:
<input type="radio" name="radio" id="academic" value="1"<?php
if ($row_EventInfo['academic'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['academic'] = '';}
?>>
<label for="academic">Academic</label><br />
<input type="radio" name="radio" id="personal" value="1"<?php
if ($row_EventInfo['personal'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['personal'] = '';}
?>>
<label for="personal">Personal</label><br />
<input type="radio" name="radio" id="diversity" value="1"<?php
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
?>>
<label for="diversity">Diversity</label><br />
What I'm trying to do is this. I have a column in my database table for each radio button because we have to have their inputs separately. However, I want them to only be able to select one of the buttons at a time. I changed all the names to be the same ("radio"), but since PHP MYSQL uses the names to know where to post the information in the table it doesn't know where to post.
Is there any way to create an if statement to tell it to only allow one button at a time to be selected and keep the inputs separate for the database table?
Please let me know if you need clarification. Thanks!
set the value to the column name, eg
<input type="radio" name="radio" id="diversity" value="diversity"
on the php end, simply do something like
$sql = "UPDATE table SET {$_POST['radio']} = 1";
this is in a form, right?
Then you get the data in the POST. I wouldn't set the value to 1, set the value to the value you want to select, e.g.
Then in the post, get the value and use this to set the data in the DB.
Hope this helps!
BR,
TJ
If you use:
<input type="radio" name="radio" id="diversity" value="value1">
<input type="radio" name="radio" id="diversity" value="value2">
<input type="radio" name="radio" id="diversity" value="value3">
<input type="radio" name="radio" id="diversity" value="value4">
it will be available in $_POST['radio']; with what ever radio button you selected. eg.value3 dont confuse yourself by naming input types with the same name
If you use:
<input type="radio" name="myradio" id="diversity" value="value1">
It will be available in $_POST['myradio'];
and so on
plus you may want to look into using the ternary operator
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
into
echo ($row_EventInfo['diversity'] == '1') ? ' checked="checked"':'';