Checked HTML from PHP - php

I have code:
<?php
if ($user['x'] == 1) { $x_checked = ' checked'; } else { $x_checked = ''; }
if ($user['y'] == 1) { $y_checked = ' checked'; } else { $y_checked = ''; }
if ($user['a'] == 1) { $a_checked = ' checked'; } else { $a_checked = ''; }
if ($user['b'] == 1) { $b_checked = ' checked'; } else { $b_checked = ''; }
if ($user['c'] == 1) { $c_checked = ' checked'; } else { $c_checked = ''; }
[...]
?>
<input name="a" type="checkbox"<?php echo $a_checked; ?> />
<input name="b" type="checkbox"<?php echo $b_checked; ?> />
<input name="c" type="checkbox"<?php echo $c_checked; ?> />
[...]
and i have too long code (others same lines). How shortcode to this?

Just check in the input HTML:
<input name="a" type="checkbox" <?php echo ($user['a'] == 1) ? 'checked' : '' ?> />
<input name="b" type="checkbox" <?php echo ($user['b'] == 1) ? 'checked' : '' ?> />
<input name="c" type="checkbox" <?php echo ($user['c'] == 1) ? 'checked' : '' ?> />
If the values can only be 0 or 1 (or maybe more than 1 if you want that checked) then it is shorter:
<?php echo $user['c'] ? 'checked' : '' ?>
If you're going to have a $user element for each checkbox then loop it:
<?php foreach($user as $key => $val) { ?>
<input name="<?php echo $key ?>" type="checkbox" <?php echo $val ? 'checked' : '' ?> />
<?php } ?>
From your comment it appears you may be echoing, if so then just:
foreach($user as $key => $val) {
$checked = $val ? 'checked' : '';
echo '<input name="'.$key.'" type="checkbox" '.$checked.'/>';
}

Welcome to Stackoverflow!
Foreach loops and arrays are in this case your best friends, this is how I usually do it.
<?php
$input_name = array('a', 'b', 'c', 'd');
input_data = '';
foreach ($input_name as $value) {
if ($user[$value] == 1) {
$input_data .= '<input name="'.$value.'" type="checkbox" checked>';
} else {
$input_data .= '<input name="'.$value.'" type="checkbox">';
}
}
?>
Echo the results in the HTML part:
<?=$input_data?>

<?php
$fields = [
'a',
'b',
'etc'
];
foreach ($fields as $field){
if($user[$field] == 1){
$checked = 'checked';
}else{
$checked = '';
}
print('<input name="'.$field.'" type="checkbox" '.$checked.' />');
}
?>

Related

For loop display radio buttons with first one checked

I have a for loop that displays radio buttons and I want the first one to display as checked. But when I put a if statement inside the for loop for this the page nevers loads. Any ideas?
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if($mainNO = 0){ echo 'checked="checked"'; } ?>/>
<?php } ?>
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo ' checked="checked" ';
} ?>/>
<?php } ?>
you use = where you should use ==
u are using assingment operator in comparision statement
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo " checked";
} ?>/>
and in HTML5 you can use checked only
<input type="checkbox" checked>
// Note: You used single = in if condition that is wrong, it will create indefinite loop . Tested code.
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) {
// Checked if value is 0
if($mainNO == 0){ $checked = 'checked="checked"'; }else { $checked =''; };
echo "<label for='mains".$mainNO."' class='radiobutton'>".$mains[$mainNO]."</label>";
echo "<input type='radio' name='mains' id='mains".$mainNO."' value='".$mainNO."' $checked />";
}

Radio button checked not working in php

This is my code :
<td>
<?php
if ($adPropertyPayment == "Direct") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPC") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPM") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checked ?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checked ?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checked ?> onclick="showAmount('CPM');" />CPM
</td>
Checked is not working. I am getting the value of $aspropertypayment in POST.
Use this
<?php
$adPropertyPayment = $_POST['payment'];
if ($adPropertyPayment == "Direct") {
$checkedDir = "checked = 'checked'";
} else {
$checkedDir = "";
}
if ($adPropertyPayment == "CPC") {
$checkedCpc = "checked = 'checked'";
} else {
$checkedCpc = "";
}
if ($adPropertyPayment == "CPM") {
$checkedCpm = "checked = 'checked'";
} else {
$checkedCpm = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checkedDir;?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checkedCpc;?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checkedCpm;?> onclick="showAmount('CPM');" />CPM
Try writting only $checked = 'checked'; in your if statement.

if statement is equal to a value

I have a result(string) of 1,1,0,0 - These come from $sub_array['state']
Currently all of my check boxes are checked. How can I code the code below so that if its 1 its checked else its not? as the current code gives them all 'checked'
<?php
foreach($assoc_categories as $sub_array)
{
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}
?>
Change:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
To:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
} else
{
$checked_state = "";
}
Basically, you are not clearing the previous value as the loop continues.
Alternatively, you could use:
$checked_state = ($sub_array['state'] == 1) ? " checked='checked'" : "" ;
You forget to reset checked_state or reset it to '' if $sub_array['state'] is equal to 0.
<?php
$assoc_categories = array(
array('state'=>1, 'cat_id'=>1, 'name'=>'one', 'sorder'=>1),
array('state'=>1, 'cat_id'=>2, 'name'=>'three', 'sorder'=>2),
array('state'=>0, 'cat_id'=>3, 'name'=>'four', 'sorder'=>3),
array('state'=>0, 'cat_id'=>4, 'name'=>'five', 'sorder'=>4),
);
foreach($assoc_categories as $sub_array)
{
$checked_state = $sub_array['state'] == 1 ? " checked='checked'" : '';
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}

How do I make values stay in chekbox?

How do I make the values stay in checkboxes?
my problem is when I submit the form the values do not stay (in the form).
Below is my code :
Mca<input type="checkbox" name="qual[]" id="Mca" value="Mca"
<?php if($qual == "Mca") { echo ' checked="checked"' ; } ?>>
Mtech<input type="checkbox" name="qual[]" id="Mtech" value="Mtech"
<?php if($qual == "Mtech") { echo "checked"; } ?>>
Btech<input type="checkbox" name="qual[]" id="Btech" value="Btech"
<?php if($qual == "Btech") { echo "checked"; } ?>>
Try this because $qual is an array.
/* Your $qual should be */ <?php $qual = $GET['qual']; ?>
<?php if($qual == "Mca")
must be changed to :
<?php if(in_array("Mca",$qual)
Try using better code for your form:
<?php
$checkboxes = array('Mca', 'Mtech', 'Btech');
foreach($checkboxes as $k => $v){
echo '<input '.($v==$qual[$k]? 'checked="checked" ': '').'type="checkbox" name="qual[]" id="'.$v.'" value="'.$v.'">';
}
?>

Populate Checkbox from Database Query

I'm trying to check a check box, if the value for that field is 1 in the database.
I have:
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK) {
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
$checked = $Priorityresult['Priority'];
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1"
<?php if ($checked == 1) echo ' checked'; ?> />
but not getting any joy, any ideas?
Try this:
You were not using the row returned by the query...
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK)
{
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
//$checked = $Priorityresult['Priority']; // <------ this is where you went wrong...
$checked = $row['Priority']; // <------ this will fix where u went wrong!
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1){echo ' checked'; }?>
You should use
<?php if ($checked == 1){echo "checked='checked'"; }
and also
$checked = $Priorityresult['Priority'];
to
$checked = $row['Priority'];
I think you have one mistake... Try this
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($row['Priority'] == 1) echo ' checked'; ?> />
Try it this way
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1) echo "checked='checked'"; ?> />
change
<?php if ($checked == 1) echo ' checked'; ?>
to
<?php if ($checked == 1) echo ' checked="checked"'; ?>
and $checked = $Priorityresult['Priority']; to $checked = $row['Priority'];
It should be checked="checked"
<?php if ($checked == 1) echo "checked='checked'"; ?>

Categories