php not echoing back radio value - php

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?>

Related

Displaying radio button choice php?

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

Array Search and Checkboxes

i cant seem to make the checkbox appear as checked if this statement turns out to be true. the array passed is working fine and everything is in palce but i dont seem to find a way where i can make the checkbox checked if the value of BSS is there
<?php if(isset($_GET['cng']) && array_search("BSS", $scharr))
{
echo "checked=''";
};
?>/>
BSS
</label>
try below code:
<label for="one">
<input type="checkbox" name="school[]" value="BSS" <?php echo isset( $_GET['cng'] ) && in_array("BSS", $scharr) ? 'checked' : ''; }; ?> />
BSS </label>
<input type="checkbox" <?php if(isset($_GET['cng']) && isset($scharr['BSS'])) echo "checked"?> />
Try this code
<input type="checkbox" <?php if(isset($_GET['cng']) && array_search("BSS", $scharr)) { echo "checked";}>

Checkbox is checked when go to Edit page - PHP HTML

I have a situation where I have create a new information after submit a new info, the information is a success into the database. But when I click on the 'Edit' page, all other information is displayed. But for the input type checkbox, the information does not display at all.
<tr>
<td>Transaction</td>
<td>
<div class="checkbox check-default check-success">
<input id="f1" type="checkbox" value="1" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
<label for="f1"></label>
</div>
</td>
</tr>
I wonder what I am missing. Please help, thanks.
Remove the value=1 in your code.
Use this code below
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
You specified the value of a default input value = 1, removes value = 1
try
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>

Show/hide input after click in checkbox

I'm trying to Show/hide my input after check/uncheck a checkbox but I getting this info from DB so, I'm using PHP to add 'checked' attribute to my input.
jQuery code is working, but when I refresh the page, my input doesn't show even with attribute checked "enabled".
PHP code
<label>Send cash?</label>
<input class="reg_cash" type="checkbox" name="reg_cash" value="1"
<?php echo ($configs->reg_cash) ? 'checked' : '' ?>>
<div class="reg_cash_amount">
<label>Amount of cash</label>
<input type="text" name="reg_cash_amount" id="coupon_field"/>
</div>
Jsfiddle: https://jsfiddle.net/qmmg3qwo/
try this to hide/show the input field based on database values.
php code
<label>Send cash?</label>
<input class="reg_cash" type="checkbox" name="reg_cash" value="1"
<?php echo ($configs->reg_cash) ? 'checked' : '' ?>>
<?php if($configs->reg_cash) { ?>
<div class="reg_cash_amount">
<label>Amount of cash</label>
<input type="text" name="reg_cash_amount" id="coupon_field"/>
</div>
<?php } ?>
Jquery code
$(".reg_cash").click(function () {
if ($(this).is(":checked")) {
$(".reg_cash_amount").show();
} else {
$(".reg_cash_amount").hide();
}
});

storing checkbox on multi step form with php session

i have a form in three steps, from step 1 to step two there is a checkbox, i would like to keep the checkbox value in a session and also to show if it's checked or unchecked even if the user goes to step 3 of the form then comes back to step two,
right now on form submit i have all the $_POST vars in $_SESSION vars but i cant make it work with checkboxes, this is what i got right now:
<input type="checkbox" value="<?php echo isset($_POST['afficher_ddn'])? "1":"0"; ?>"
style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
but this doesnt work.
<input type="checkbox" value="1" style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo isset($_SESSION['afficher_ddn']) && $_SESSION['afficher_ddn'] == "1" ? 'checked="checked"' : ''; ?> />
For one, you don't change the value of the checkbox. If it is not checked it won't pass to the post vars. See here: Does <input type="checkbox" /> only post data if it's checked?
Now, the current standard for checked is checked="checked" I have no idea where or why this standard became one at the firm I work for, but it's what we do so I relay that here.
If the code provided here does not work for you, I would var_dump($_SESSION) to make sure it's set as well as check the actual HTML in something like firebug (firefox) or developer tools in chrome. Sometimes goofiness happens and checked="checked" is actually set in the html but doesn't display in browser. In those times I usually yank out some hair and clear caches. then it usually clears up.
Your code is fine.Possibly there is some problem in passing the the value in session as i am checking the code manually it is working perfectly:
use print_r($_SESSION) for testing and post the output;If possible provide all the forms:
Try Below Code:
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
try this example separetly
Make a page test.php as:
<?php
session_start();
?>
<form methos='post' method='post' action='test1.php'>
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> /><br>
Name:<input type='text' name='f1' value='<?php echo $_SESSION['f1'] ?>' ><br>
Select:<select name='s1'>
<option value="" <?php echo $_SESSION['s1']=="" ? "selected" : ""; ?>></option>
<option value="1" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>1</option>
<option value="2" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>2</option>
</select><br>
Test:
<textarea name='tex1'><?php echo $_SESSION['tex1']; ?></textarea>
<input type='submit' value='submit'>
</form>
Make a second page test1.php as:
<?php
session_start();
print_r($_POST);
echo $_SESSION['afficher_ddn']=$_POST['afficher_ddn'];
echo $_SESSION['f1']=$_POST['f1'];
echo $_SESSION['s1']=$_POST['s1'];
echo $_SESSION['tex1']=$_POST['tex1'];
?>
<form methos='post' action='third.php'>
<a href='test.php'>Step1</a>
</form>
and Check

Categories