Check box will not update with a tick from mysql - php

I'm creating a form that has two checkboxes yes and no. I have added a yes or no into a mysql field called Completed.
The problem I have is getting the tick boxes to show a tick if the field that has a Yes or a No.
So depending what is in the field Completed "Yes" or "No" the tick boxes should be ticked or not ticked.
My code is like this:
<input type="checkbox" name="Completed" Completed="Yes" value="<?php echo $stuff["Completed"]; ?>" <?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> > Yes</td>
<input type="checkbox" name="Completed" Completed="No" value="<?php echo $stuff["Completed"]; ?>" <?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> > No</td>

Check boxes don't strike me as an appropriate control for this type of information. Have you considered using radio buttons instead? In any case, the answer to your question depends on how you are storing the data in the database. If it is a simple "Yes" or "No", then try something like the following:
<input type="radio" name="Completed" value="Yes" <?php echo $stuff["Completed"] == 'Yes' ? 'checked="checked"' : ''; ?> />

<?php echo $stuff["Completed"] ? 'checked=" checked"' : ''; ?> will always return true because you check for 'Yes' and 'No'. This values are true for php. You have to set the values 0 or 1 in db or check do something like this:
<?php echo $stuff["Completed"] == 'Yes' ? 'checked=" checked"' : ''; ?>
...
<?php echo $stuff["Completed"] == 'No' ? 'checked=" checked"' : ''; ?>

Related

Match Array Value with input field inside nested while Loop

It's a really silly problem.i don't know how i get this but i get.
i have a array and i wants to match array values with checkbox which came from while loop.So how is it possible.
//Array that i want to match with checkbox
$filter = explode(',', $getproduct->specification_filter);
<table class="table" style="background-color: white;">
<tbody>
<?php
while($fch = $allfilter->fetch_array()){
?>
<tr>
<th><?=$fch[2]?></th>
<?php
$sqlbv="SELECT * FROM product_filter where idd='$fch[4]'";
$resultbv=$conn->query($sqlbv);
while($rowbv = $resultbv->fetch_array()){
?>
<td>
<label class="ckbox ckbox-danger">
<input type="checkbox" value="<?=$rowbv[0]?>" name="pfilter[]">
<span> <?=$rowbv[3]?></span>
</label>
</td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
Now i wants to check Checkbox field if $filterarrays value match with checkbox value. So how can i do that ?
since your $filter is an array use in_array
<input type="checkbox" value="<?=$rowbv[0]?>" <?= in_array($rowbv[0], $filter)? 'checked':'' ?> name="pfilter[]">
for more details see in_array.
Just use in_array() to check this. If its true and marked checked.
<input type="checkbox" value="<?=$rowbv[0]?>" name="pfilter[]" <?php echo in_array($rowbv[0],$filter) ? 'checked="checked"' : '';?>>
Hope this could help you.
In input checkbox, you can check adding attribute 'checked'.
For that, you can write
< input type="checkbox" < ?=($rowbv[0] ? 'checked' : '')?> name="pfilter[]"/>
else
< input type="checkbox" < ?php echo $rowbv[0] ? 'checked' : '';?> name="pfilter[]"/>

mpdf showing a radio button and checkbox selected

I have a quick question.
I creating an pdf output when the user clicks on the button. However the problem I'm having is the radio and checkbox. I tried to use the standard
($answer == 'What the value is' ) ? 'checked':''
however when it is outputted to a pdf, it doesn't show the radio button or checkbox selected. Is there way I can have the radio buttons or checkbox's selected in an pdf output?
Thank you
Ok, I have found a solution. What I did was I put the the checkbox and radio button in if statement and I did put the code in the following format:
<input type="radio" checked="checked"> Yes
That did the trick..
try this:
if ($answer = "1"){
$output .= 'Yes <input type="checkbox" checked /> ';
}
else{
$output .= 'Yes <input type="checkbox" /> ';
}
Where is 1 true. I assume that, you get these values from db. In this cycle check these values if it checked.
<input type="radio" value="1" <?php $data->input_11 == 1 ? 'checked="checked"' : null ?> > Yes
<input type="checkbox" value="1" <?php $data->input_11 == 1 ? 'checked="checked"' : null ?> > Yes
Its Work for me

Ternary Logic PHP on Radio Button

I have a form that i pull data from the database and fill in all the fields for a user to edit an existing record. It looks like this currently to set the radio button:
<input type="radio" id="statusActive" value="1" name="status" <?php if ($departmentData->thresholdActive == "1"){ echo 'checked'; }else{ echo ''; } ?>> Active
What would be the Ternary Logic approach to say:
If POST is true, us the post data else use the one from the database?
<input type="radio"
id="statusActive"
value="1"
name="status"
<?php echo ($departmentData->thresholdActive == "1") ? 'checked' : ''; ?>
>
A ternary expression using the following
if ($departmentData->thresholdActive == "1")
{
echo 'checked';
}
else
{
echo '';
}
Would be
echo $departmentData->thresholdActive == "1" ? 'checked' : '';
"Ternary logic" - you probably want
<?php echo ($departmentData->thresholdActive == "1")? 'checked':'';?>
But if you first want to check whether POST is true, it would be cleaner to so something like
(isset($_POST))?$_POST['index'] : $dbValue;
Where 'index' refers to a specific post variable (which I assume is present when POST is set; you didn't specify whether to check for a specific POST variable so it's hard to guess); and assumes that you know how to set $dbValue with a "value from the database"

check if input checkbox checked not working

form :
<label for="sitecheck">
<span style="font-weight:bold;">סגור אתר זמנית:</span>
</label>
<input name="sitecheck" type="checkbox" id="sitecheck" onClick="validateSitec();" <?php if($data['sclosed'] == 'true'){echo 'checked = "checked"';}; ?> /><span style="font-weight:bold;">סגור אתר ורשום הודעה זמנית</span><br>
<input type="text" name="closedmsg" id="closedmsg" style="width:440px;height:120px;<?php if($data['sclosed'] == 'true'){echo '';}else{ echo 'display:none;'; }; ?>" value="<?php echo $data['csitemsg']; ?>" /><span id="sitemsg_error"></span>
php :
if(isset($_POST['sitecheck']))
{
$sitecheck = 'true';
}else{
$sitecheck = 'false';
}
any idea why its not working ?
i trying to determine if checked or not and update on Database.. anyidea why its alwys true even if i uncheck?
EDIT :
fixed by my own was ajax post wrong , i should val the checked box like that :
var sitecheck = $('#sitecheck').is(':checked') ? $('#sitecheck').val() : '' ;
so now working thanks.
Add value param to your checkbox. Value will be send if checkbox is checked.
<input name="sitecheck" type="checkbox" id="sitecheck" value="1" ....

A Simple HTML Checkbox with PHP Problem

I have a field in my update form called approve which is using the html checkbox element. now i am querying the approve value from the database which will hold the binary value (0 or 1), i want the checkbox to perofrm the following actions in condition.
a) While Querying from database.
1)if the value of active is 1 then it should be checked by default and also it should hold the value 1 to process it to update
2)the same applies for 0, if the value is zero then it is unchecked and will hold the value 0 to process
P.S: I want to use this for updating the form not inserting.
Do it like this:
PHP embedded in HTML way:
<input name="chk" type="checkbox" value="<?=$value?>" <?php if($value==1) echo 'checked="checked"';?> />
Pure PHP way:
<?php
echo '<input name="chk" type="checkbox" value="'.$value.'"';
if($value == 1)
echo ' checked="checked" ';
echo '/>';
?>
Just a 1-line shorter version ;)
<?php echo "<input name=\"chk\" type=\"checkbox\" value=\"$value\"".( ($value == 1) ? " checked=\"checked\"" : "" )." />"; ?>
Like this:
<input type="checkbox" value="<?php echo $row['approved']; ?>" <?php if($row['approved'] == 1): echo 'checked="checked"'; endif; />

Categories