Populate Checkbox from Database Query - php

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

Related

Checked HTML from 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.' />');
}
?>

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.'">';
}
?>

Update database with checkbox php mysql

I want to update the database with a checkbox in checked state.
If it is checked then update the database with 1.
Else if it is unchecked then update it with 0.
It works fine but it doesn't work if unchecked.
<?php
include('lib/db.php');
$facebook_id ="10001088";
$query1 = "SELECT `video`,`quran`,`medical`,`groups` FROM `man_facebook`.`users` WHERE `facebook_id`='$facebook_id'";
$result1 = mysql_query($query1);
while($result = mysql_fetch_array($result1))
{
$video = $result['video'];
$quran = $result['quran'];
$medical = $result['medical'];
$groups = $result['groups'];
echo $video;
// echo $quran;
?>
<form method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>" >
<input type="checkbox" name="video" id="video" value="<?echo $video;?>" <?php
if($video == '1'){
echo "checked='checked'";
}
else {}
echo "/>"
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
if (isset($_POST['submit']))
{
if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
$video1 = isset($_POST['video']) ? '1' : '0';
echo $video1;
$query = mysql_query("UPDATE `man_facebook`.`users`
SET `video` ='$video1'
WHERE `facebook_id`='$facebook_id'");
$video = $video1;
echo '<meta http-equiv="refresh" content="0" />';
}
}
//echo $query;
//header("Location: updatesql.php");
?>
Can I also use jquery to update it smoothly?
I find this part a bit weird:
if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
$video1 = isset($_POST['video']) ? '1' : '0';
You first check if it is numeric and then check if it is set. If it wasn't set, that if would become false automatically. In other words, $video is always 1. Assuming $video can be only true/false (or maybe it comes as "checked"/"unchecked", not really sure), use it like this:
$video1 = ($video ? '1' : '0');
Hopefully I spotted the issue successfully :)
UPDATE
<input type="checkbox" name="video" id="video" value="video" <?php
if($video == '1'){
echo "checked='checked'";
}
echo "/>";
...
if (isset($_POST['submit']))
{
echo $_POST['video']; // again, please tell what it outputs here!!!
$video1 = (($_POST['video'] == "video") ? '1' : '0');
checkbox update
if (isset($_POST["send"]) && $_POST["send"] == "ok") {
foreach ($_POST['membre'] as $id => $data) {
mysql_query("UPDATE tablename SET param1='" . $data['param'] . "' WHERE id='" . $id . "'");
}
}
<form>
<input type="hidden" name="send" value="ok" />
<td class="cel2" style=" width: 100px;">
<input name="membre[<?php print $val['id']; ?>][param]" type="checkbox" style="margin-top: 7px; width: 30px;" value="1" id="param" <?php if ($val['p'] == '1') print 'checked'; ?> />
</td>
</form>

Categories