Radio Button Array - Need Help - php

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"':'';

Related

PHP form check box from db table (beginner)

Had an a question answered on a different post; now I need to solve the next step! :)
We have a DB that has secondary_phone_number as either a 1 or a 0.
In our View (MVC); I have
{
$secondaryNumber = $company->getSecondaryPhoneNumber();
if ($secondaryNumber = 1) {
$data['secondaryphonenumber'] = TRUE;
} else {
$data['secondaryphonenumber'] = FALSE;
}
}
Then in our template (HTML), I am trying to display either the checkbox as being checked if Yes (1) or No (2), depending on the DB entry:
<label>
<input type="checkbox" name="secondary_phone_number" value="1" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
No
</label>
The problem is, both checkboxes are displaying as Checked, no matter what the DB says. Any thoughts?
I'm not sure what kind of MVC framework you're using or you made your own framework, but your approach doesn't really care what is in secondaryphonenumber when generating checkbox
try something like this
<label>
<input type="checkbox" name="secondary_phone_number" value="1" <?=$secondaryphonenumber?"checked":""?>/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" <?=$secondaryphonenumber?"":"checked"?>/>
No
</label>
But I'd suggest using radio button instead because checkbox allows user to select both options.
Try this one..
<label>
<input type="checkbox" name="secondary_phone_number" value="1" <?php if($data['secondaryphonenumber'] == TRUE){?>checked="checked"<?php }?>/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" <?php if($data['secondaryphonenumber'] == FALSE){?>checked="checked"<?php }?>/>
No
</label>
Using radio button will be a better option..
Try this :
$secondaryNumber = $company->getSecondaryPhoneNumber();
if ($secondaryNumber = 1) {
$data['secondaryphonenumber'] = TRUE;
} else {
$data['secondaryphonenumber_false'] = TRUE;
}
<label>
<input type="checkbox" name="secondary_phone_number" value="1" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" {{#secondaryphonenumber_false}}checked="checked"{{/secondaryphonenumber_false}}/>
No
</label>

Using checkbox and Input field for inserting into DB

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"];
}

Checked radio that is insert in database when select

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}>";
}
?>

keeping radio button value after post

HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>

Remember "Radio" value when submitting form to self on php page?

On a text input, this is how you would "remember" the value the user entered if the form gets submitted to itself, for say a picture upload tool which requires that, so the user wont have to type everything into the form again after uploading a picture.
<input type="text" id="text" name="text" value="<?php echo #$_POST['text'];?>">
but how is this done when it comes to radios?
I would prefer not to create the actual radio with php, I would prefer another solution. But in the end I would go with the easiest! Javascript is also okay to use here!
Thanks
<input type="radio" id="radio_button_1" name="radio_button" value="1"<?php if($_POST['radio_button'] == 1) { print ' checked="checked"'; } ?> />
<input type="radio" id="radio_button_2" name="radio_button" value="2"<?php if($_POST['radio_button'] == 2) { print ' checked="checked"'; } ?> />
To short further, you can write this statement as:
<input type="radio" id="radio_button_2" name="radio_button" value="2" <?=isset($_POST['radio_button']) ? "checked":"" ?> />
I arrived here via Google and the acccepted answer is missing the if(isset($_POST['radio_button'])) check first, which could be handy in some cases.
<input type="radio" id="radio_button_1" name="radio_button" value="1"<?php if(isset($_POST['radio_button']) && $_POST['radio_button'] == 1) { echo ' checked="checked"'; } ?> />
<input type="radio" id="radio_button_2" name="radio_button" value="2"<?php if(isset($_POST['radio_button']) && $_POST['radio_button'] == 2) { echo ' checked="checked"'; } ?> />

Categories