cannot make radio buttons sticky - php

So I managed to make my radio buttons sticky, but what happened with this code was the user can now select more than one option (they are supposed to only be able to select one), and, somehow once you select an option you cannot un-select it.
This is my code right now:
How did you hear about me?<br/>
<input type="radio" name="response_fb" value="true"
<?php if (isset($_POST['response_fb']) == 'true') echo ' checked'; ?>>Facebook/online<br>
<input type="radio" name="response_wordofmouth" value="true"
<?php if (isset($_POST['response_wordofmouth']) == 'true') echo ' checked'; ?>>Word of mouth<br>
<input type="radio" name="response_other" value="true"
<?php if (isset($_POST['response_other']) == 'true') echo ' checked'; ?>>Other
Can anyone help?
thanks in advance!

<input type="radio" name="response" value="facebook"
<?php if (isset($_POST['response']) and $_POST['response'] == 'facebook') echo ' checked'; ?>>Facebook/online<br>
<input type="radio" name="response" value="wordofmouth"
<?php if (isset($_POST['response']) and $_POST['response'] == 'wordofmouth') echo ' checked'; ?>>Word of mouth<br>
<input type="radio" name="response" value="other"
<?php if (isset($_POST['response']) and $_POST['response'] == 'other') echo ' checked'; ?>>Other
U must set same name and different value !

The name attr of all your radio buttons needs to be the same. That is how HTML determines that only one of the group can be selected.
EDIT: I think this is more of what you are shooting for
<?php if (isset($_POST['response']) == 'true') echo '$_POST['response'] was checked'; ?>
<input type="radio" name="response" value="facebook">
<input type="radio" name="response" value="word_of_mouth">
<input type="radio" name="response" value="other">

Related

Retrieve mysql value from php for radio buttons

As trivial as it can seems, I'm having problems retrieving values for radio buttons from MySql database through PHP. It's my first learning project so I'm trying my best
Question has already been asked but I found no useful answer.
The php code does a simple "Select *" so I retrieve all the fields.
This is the php code
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "A") { echo "true"; }?> value="A">A
<input type="radio" name="Owner" checked=<?php if($row['Owner'] = "B") { echo "true"; }?> value="B">B</br></br>
and I retrieve the values with mysqli_fetch_array().
This is the result:
As you can see the label retrieves the correct value, the radio buttons not.
I've already tried putting == instead of = and putting ' instead of " but I don't know why the checkbox "B" is checked, since Owner value is A.
Also, if there are any best practices which are better than this, you're welcome.
The HTML attribute checked should not get a value, its mere presence indicates that the radio button is checked. So do this:
<label>Owner: <?php echo $row['Owner']; ?></label></br>
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {echo "checked"}?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {echo "checked"}?> value="B">B
Or using the more compact short echo tag <?= .. ?> and ternary operator:
<label>Owner: <?=$row['Owner']?></label></br>
<input type="radio" name="Owner" <?=$row['Owner']=="A" ? "checked" : ""?> value="A">A
<input type="radio" name="Owner" <?=$row['Owner']=="B" ? "checked" : ""?> value="B">B
Note that you need double equals signs for comparisons.
try this code not = but use ==
<input type="radio" name="Owner" <?php if($row['Owner'] == "A") { echo "checked"; }?> value="A">
$gender=$row['gender'];
<input type="radio" name="gender" <?php if($gender=="Male"){?> checked="true" <?php } ?> />Male
<input type="radio" name="gender" <?php if($gender=="Female"){?> checked="true" <?php } ?>/>Female
$owner=$row['owner'];
$owners= ['A'=>'', 'B'=> ''];
$owners[$owner] = 'checked';
<input type="radio" name="Owner" <?php echo $owners['A']?> value="A">A
<input type="radio" name="Owner" <?php echo $owners['B']?> value="B">B
i think it's easy and simple and so useful if you have many values in DB.
i tried the answer given by #trincot but it give me errors so i have make little changes to improve the answer
<input type="radio" name="Owner" <?php if($row['Owner']=="A") {?> <?php echo "checked";?> <?php }?> value="A">A
<input type="radio" name="Owner" <?php if($row['Owner']=="B") {?> <?php echo "checked";?> <?php }?> value="B">B

How do I get checkbox as checked if value is stored in database?

I am having a problem in retrieving checkboxes from the database.
Actually I am making edit form by using the input form which insert the values in database.
I am facing problem with checkboxes and other inputs works fine.
My code looks like
<input type="checkbox" name="proizvodi[]" value="0" <?if ($row['PROIZVODI'] == '0') $checked = 'checked="checked"'; ?> checked="checked"><b>product1<b/> <input type="checkbox" name="proizvodi[]" value="1" <?if ($row['PROIZVODI'] == '1') $checked = 'checked="checked"'; ?> checked="checked"><b>product2<b/> <br/>
<input type="checkbox" name="proizvodi[]" value="2" <?if ($row['PROIZVODI'] == '2') $checked = 'checked="checked"'; ?> checked="checked"><b>product3</b> <br/>
To make it more clear, I want only checkboes that are stored in db to be checked. for ex if value 1 is stored in db, product1 checkbox will be checked, if value 1 is no stored then check box will be not checked.
I appreciate any help or advice.
Thanks in advance!
How about this? You need to echo the 'checked="checked"' and do not assign it to a variable
<input type="checkbox" name="proizvodi[]" value="0" <?php if (strpos($row['PROIZVODI'],'0') !== false) echo 'checked="checked"'; ?>><b>product1<b/>
<input type="checkbox" name="proizvodi[]" value="1" <?php if (strpos($row['PROIZVODI'],'1') !== false) echo 'checked="checked"'; ?>><b>product2<b/> <br/>
<input type="checkbox" name="proizvodi[]" value="2" <?php if (strpos($row['PROIZVODI'],'2') !== false) echo 'checked="checked"'; ?>><b>product3</b> <br/>
Write your rows like this don't make it any harder than it is:
<input type="checkbox" name="proizvodi[]" value="0" <?php if ($row['PROIZVODI'] == '0') { ?> checked="checked" <?php } ?> /><b>product1<b/>

Radio Button Array - Need Help

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

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