My code works fine after the form fill out all values are print accept my gender value.
Here in the code below is an example imagine firstName,lastName.... the $row[dob] value prints out successfully.
But the option for the gender is not printing is there some array function i need to use for this to have the option selected bu the user print back on the screen when only display the details of the user (similar to a sticky form)?
<li><label for='dateOfBirth'>DOB:</label>
<input type='date' name='dateOfBirth' value = $row[dob] min='1900-01-01' max= '2015-01-01'/></li>
<li>
<input type='radio' name='gender' value=$row[gender]'>
<label for='male'>Male</label><br>
<input type='radio' name='gender' value='female'>
<label for='female'>Female</label><br>
</li>
you need to use the checked attribute
if the value that comes from $row['gender'] is male check the male radio otherwise check the female
like this
<li>
<input type='radio' name='gender' <?= $row[gender] == 'male'? 'checked':'' ?> value='male'>
<label for='male'>Male</label><br>
<input type='radio' name='gender' <?= $row[gender] == 'female'? 'checked':'' ?> value='female'>
<label for='female'>Female</label><br>
</li>
You shouldn't set the value of the radio inputs, instead determine if the value is checked.
Example:
<li>
<input type='radio' name='gender' value='male' <?= $row['gender'] === 'male' ? 'checked' : null ?>>
<label for='male'>Male</label><br>
<input type='radio' name='gender' value='female' <?= $row['gender'] === 'female' ? 'checked' : null ?>>
<label for='female'>Female</label><br>
</li>
NOTE: Your condition <?= $row['gender'] === 'female' ? 'checked' : null ?> may vary depending on how you template your HTML.
Sandbox
Related
hello
i am not being able to submit these value in my controller.
the code is as follows
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="1" checked="" required="" />
<span class="lbl"> YES</span>
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="0"/>
<span class="lbl"> NO</span>
For multiple radio use array in name attribute with id as a index key like this
name="status[<?php echo $data['LEAVE_ID']?>]"
You can achieve it using below code, you just have to add css.
<?php
foreach($dataArrry as $data) {
echo $_POST['status.' . $data['LEAVE_ID']]."<br/><br/>";
}
?>
I cannot get the radio value's to echo back as checked within the conditional block, it's got to be an issue with my syntax. I know you don't have the full code but I can assure you 100% that the conditions are all true. What am I missing?
<?php if(!empty($userRow[insurance_name2])):?>
<input class="form-control" type="text" id="insurance-name2"
name="insurance-name2"
placeholder="Insurance Name"
value="<?php echo $userRow[insurance_name2] ?>">
<input name="insurance-network-option2" type='radio'
value="in-network" <?= ($userRow['insurance_option2'] == "in-network") ? 'checked' : '' ?>>In-Network
<input name="insurance-network-option2" type='radio'
value="out-of-network" <?= ($userRow['insurance_option2'] == "out-of-network") ? 'checked' : '' ?>>Out-Of-Network
<br>
<br>
<?php endif?>
try this
<?php if(!empty($userRow['insurance_name2'])):?>
<input class="form-control" type="text" id="insurance-name2" name="insurance-name2" placeholder="Insurance Name" value="<?= $userRow['insurance_name2']; ?>" />
<input name="insurance-network-option2" type='radio' value="in-network" <?= ($userRow['insurance_option2'] == "in-network") ? 'checked' : '' ?>/>In-Network
<input name="insurance-network-option2" type='radio' value="out-of-network" <?= ($userRow['insurance_option2'] == "out-of-network") ? 'checked' : '' ?> />Out-Of-Network
<?php endif ?>
You did forget some quotes and to close the input tag. which is what I changed.
If it doesn't solve your problem let me know what errors you're getting.
Realized my issue. Beneath I had a hidden div containing the same code as the block within the php conditional. Reason being, if the element exists in the database, I wanted it to display. Otherwise, if I user clicked a certain button to add an insurance name and network option, it would remove the hidden field.
So I rearranged the code like so, and called removeAttribute method from within the php block. Works perfectly.
<div class="hidden" id="insurance2">
<input class="form-control" type="text" id="insurance-name2"
name="insurance-name2"
placeholder="Insurance Name"
value="<?php echo $userRow[insurance_name2] ?>">
<!-- <br>-->
<input name="insurance-network-option2" type='radio'
value="in-network" <?= ($userRow['insurance_option2'] == "in-network") ? 'checked' : '' ?>>In-Network
<input name="insurance-network-option2" type='radio'
value="out-of-network" <?= ($userRow['insurance_option2'] == "out-of-network") ? 'checked' : '' ?>>Out-Of-Network
<br>
<br>
</div>
<!--//insurance two-->
<?php if(!empty($userRow[insurance_name2])):?>
<?php echo"<script type='text/javascript'>document.getElementById('insurance2').removeAttribute('class');</script>"?>
<?php endif?>
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 try to list ALL $_POST array items using var_dump (or echo), but null value items are not displayed. If I use var_dump($_POST) null doesn't appears, but if I use var_dump($_Post["nullitem"]) null appears:
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $key."=>";
echo $value;
echo " - ";
}
echo "<br>";
echo "ck_1 "; var_dump($_POST["ck_1"]);
echo "ck_2 "; var_dump($_POST["ck_2"]);
echo "ck_3 "; var_dump($_POST["ck_3"]);
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
Only ck_2 is checked, so this example will output :
array
'ck_2' => string '1' (length=1)
'submit' => string 'Submit' (length=6)
ck_2=>1 - submit=>Submit -
ck_1 null
ck_2 string '1' (length=1)
ck_3 null
How can I include ALL $_POST values in foreach loop (I don't know how many keys nor names in $_POST array)
Thanks for help
Regards
Sorry.
The unchecked checkbox is not set, so is not member of $_POST array and does not appears
A way to get a value for unchecked checkbox is to set an hidden field with same name and id and unchecked value (like 0), so at post time if unchecked hidden value is returned :
<input type="hidden" name="cx1" value="0" />
<input type="checkbox" name="cx1" value="1" />
Thank's Midzai
I think you are including all the values of $_POST array in your foreach. The thing is, if you don't check in the checkbox the $_POST array won't contain it's key nor it's value.
checkbox i believe has only one value possible and that shows only when you "check-in" the checkbox. othervise the $_POST isn't populated with the key. Why you see NULL when you direcly query the $_POST with the specified key name (name of the checkbox that wasn't set) the key does not exist in the $_POST array and to return something it returns NULL.
If you for some obscure reason need to list all the checkboxes that were available to be chcecked in to the user, you can add
<input type='hidden' name='cbNames[]' value='ck_1'/>
<input type='hidden' name='cbNames[]' value='ck_2'/>
<input type='hidden' name='cbNames[]' value='ck_3'/>
for each of the checkboxes on your site and then list through the $_POST['cbNames'] array and query the $_POST for those:
foreach ($_POST['cbNames'] as $cbName)
print $_POST[$cbName];
Try this
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
echo "<pre>";
print_r(array_filter($_POST["ck_1"]));
echo "</pre>";
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1[]' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2[]' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3[]' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
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>