When I add a new record to my database the value of the radio button is inserted successfully. However, when I go to edit the record on my edit.php page, the radio buttons are all unchecked.
How I ensure that the corresponding radio button is checked depending on what I selected.
I've tried many different things and none work. This is the latest attempt:
<?php $prohibition = $row['prohibition']; ?>
Yes: <input type="radio" name="prohibition" value="Yes" <?php if($row['prohibition'] == "Yes") print "checked";?> /><br>
No: <input type="radio" name="prohibition" value="No" <?php if($row['prohibition'] == "No") print "checked";?> />
Based on the small snippet of code...
<?php if ($prohibition == 'Yes') { echo 'checked'; } ?> should work.
You've already defined the $prohibition variable so just check against that There's no reason to use the database output directly for the if statements.
Of course it depends entirely on the output of $row['prohibition']
What is the output of $row['prohibition']? try to replace print by echo :
<?php $prohibition = $row['prohibition']; ?>
Yes: <input type="radio" name="prohibition" value="Yes" <?php if($row['prohibition'] == "Yes") echo "checked";?> /><br>
No: <input type="radio" name="prohibition" value="No" <?php if($row['prohibition'] == "No") echo "checked";?> />
Related
I am using a radio button option in edit form. In edit form radio button checked depend from data base value.for this I am using
<input type="radio" name="active" value="active" <?php echo ($row['service']=='active')?'checked':'' ?>> Active
<input type="radio" name="active" value="deactive" <?php echo ($row['service']=='deactive')?'checked':'' ?>> Deactive
its working fine.but in edit form if anyone change this value and submit the form and he remain any validation error of other field then radio button again reset to old value.how can i kept this value.
Ex. In a user edit form deactive radio button selected from database value , then user change this button to active and edit in mobile no field also and submit the form .if there are any validaton error in mobile no field then radio button also selected previousd(deactive).how can i kept them unchanged?
my form code is
<?php include_once 'header.php';
$ra=$_SESSION['ra']
$sql="SELECT * FROM emitra_basic where uid='$ra'";
$result = $conn->query($sql);
$row=mysqli_fetch_array($result);
if(isset($_POST['update']))
{$phone = $_POST['cno'];
//validion example
if(strlen($phone)!=10) {
$flag=1;
$phoneErr = "Not a valid phone number";}.......like this other validaiton
//then insertion..
my form code are......
<form id="basic" method="post" name="basic" >
<p class="contact"><label for="RU">service</label></p>
<input type="radio" id="active" name="active" value="active" <?php echo ($row['service']=='active')?'checked':'' ?>/> Active
<input type="radio" id="active" name="active" value="deactive" <?php echo ($row['service']=='deactive')?'checked':'' ?>/> Deactive
<p class="contact"><label for="contct No">Contact No</label></p>
<input id="cno" name="cno" placeholder="Contact No" value ="<?php if(isset($phone)){ echo $phone; }elseif($phoneErr=="") {echo $row['contact'];} ?>" type="text">
I am tried also using this
<input type="radio" name="active" value="active" <?php if (isset($active) && $active=="active") echo "checked"; elseif($row['service']=='active') echo "checked"; ?> > Active
<input type="radio" name="active" value="deactive" <?php if (isset($active) && $active=="deactive") echo "checked"; elseif($row['service']=='deactive') echo "checked"; ?> > Deactive
but no result found.
Its working fine in normal condition but in form updation any validation error occur then radio button checked again as to database value.
<?php if (($_POST['active']) == 'active') {echo 'checked="checked"';} ?>
I know there's "PHP keep checkbox checked after submitting form" on here, but that thread does not solve my problem, because I have multiple checkbox, what I need is when you check a checkbox, this stay checked after submit.
At the moment with this code nothing happens, I tried another way but when I check "id7" checkbox, all the checkbox get checked.
I have to know which checkbox was checked by the id that I give it, but I do not know how.
while ($fila = mysql_fetch_array($rs)) {
echo utf8_encode("
<tr>
<td>
".$fila['title']."
</td>
");?>
<td>
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
</td>
<?php
}
}
?>
First, the value of checkbox is $fila['id'] so when you are checking, use $fila['id'] instead of $fila. Also, when PHP receive array input fields with [] in their names the [] will be removed so that the correct POST variable is $_POST['checklist'].
Try changing this line:
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
to
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist']) && is_array($_POST['checklist']) && in_array($fila['id'], $_POST['checklist'])) echo 'checked="checked"'; ?> />
"name" is like a variable name - by using checklist[] you're defining an array as the variable.
Why not just name each checkbox properly? When the form is submitted, use the contents of $_POST to set each variable into the user's $_SESSION.
If the page is refreshed, use the values from $_SESSION to determine if the checkbox should be ticked. Something like (untested):
<input type="checkbox" name="vehicle" value="Bike"
<?php if (isset($_SESSION['bike_checked']) echo 'checked'; ?>> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"
<?php if (isset($_SESSION['car_checked']) echo 'checked'; ?>> I have a car<br>
<input type="submit" value="Submit">
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
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>
<?php
session_start();
if (count($_POST) > 0) {
$_SESSION['link'] = $_POST['link'];
}
?>
<form method="post">
Gmail: <input type="checkbox" name="link" value="gmail" id="gmail" <?php if ($_SESSION['link'] == 'gmail') echo "checked"; ?>>
Hotmail: <input type="checkbox" name="link" value="hotmail" id="hotmail" <?php if ($_SESSION['link'] == 'hotmail') echo "checked"; ?>>
<input type="submit" value="Spara">
</form>
Problem is if a box is checked you have to uncheck that then check another to change.
Is there a way so it unchecks the checked one when i check another? that sounds weird...
Thanks
You could just use Radio buttons instead, e.g:
<input type="radio" name="rdGroup1" value="John"> John
<input type="radio" name="rdGroup1" value="Jane"> Jane
There is a reason God created radio buttons. :)