Send a value for a checkbox when it is unchecked - php

To give you an idea of what I am after. I have items that have "info" to them, well say if I check a box to show Model, I want to be able to go in at a later date and uncheck it.
Is there anyway that you can uncheck a checkbox and have it be stored as 0 in the database?
Here is my input ( I have 10 of these )
<input type="checkbox" name="showmodel" <?php if ($showmodel == '1') echo "checked='checked'"; ?> />
this is how I am trying to update it
$query = "UPDATE `new_equip` SET `featured`='1',`showmanu`='1',`showmodel`='1' "WHERE `id`='$id' LIMIT 1";

I passed trough the same issue and i solved like this:
<input type="hidden" name="permission[<?php echo $data->permission_id; ?>]" value="off">
<input type="checkbox" name="permission[<?php echo $data->permission_id; ?>]" <?php if($data->permission == 'on') { echo 'checked'; } ?> >

Related

PHP (check or uncheck) a checkbox from Database

So I'm using a checkbox to insert data into MySql database.
When check it stores "finish"
When unchecked if stores "pending"
Now for an edit feature, I have to fetch and display the data from the database. I need the checkbox to be checked or unchecked if it is stored as finish or pending. Appreciate your help, thank you.
The code to fetch the data is shown below:
<?php
$query = ("SELECT * FROM ebook WHERE Eid='" . $Eid . "'");
$result = mysqli_query($con, $query);
$rows = mysqli_fetch_assoc($result);
?>
Displaying data and checkbox:
<html>
<input type="text" name="title" value="<?php echo $rows["Title"]; ?>">
<input class="tgl tgl-flip" id="chk" type="checkbox" name="chk" />
</html>
Put a condition echo inside the input.
<input class="tgl tgl-flip" id="chk" type="checkbox" name="chk" <?php if ($rows['status'] == 'finish') { echo "checked"; } ?>/>

checked and disabled checkbox based on mysql condition in php

I have a page with users listing in admin section. I have the approved users with disabled checkboxes and unapproved users with enabled checkboxes. Now I want to show approved users as "checked" and disable checkboxes. I searched a lot but couldn't get clue. PLease a help or suggestyion will be appreciated.
Here is my code for that,
<input type="checkbox" name="all_check[]" <?php echo $disabled ;?> value="<?php echo $row['id']; ?>" class="checkbox" id="status" ></td>
And the condition to show disabled as,
if ($status == '1') {
$disabled = "disabled";
}
Use something like :
<input type="checkbox" name="all_check[]" <?php if($row['id']==1) echo 'disabled checked="checked"';?> value="<?php echo $row['id']; ?>" class="checkbox" id="status" ></td>
Where your disabled and checked condition should be based on the value stored in your database table column. Above you can change the $row['id'] to what you have used to store the approved column.

set value on multiple row from column based on previous query php/mysql

running the following query ($box is yes or no from previous page):
$raw_results = mysqli_query($con,"
SELECT *
FROM `tbl_case`
WHERE `self_paid` ='$box'
AND `case_date_closed` BETWEEN '$startdate' AND '$enddate'
");
It displays all the fields correctly, and finishes with another checkbox
with the radio button below, if selected yes, it should set yes to paid_state on all previously returned $id'
<h1> Would you like to set these as paid to self ?
<form>
<form action="selfpaid.php" method="POST">
<input type="radio" name="state" value="yes" checked> Yes<br>
<input type="radio" name="state" value="no"> no<br>
<input type="submit" style="margin-left:80px; width:150px; height:30px;" value="Set state" />
</form>
<?php
if(isset($_POST['state']) && $_POST['state'] == "yes")
{
mysqli_query($con,"UPDATE tbl_case SET self_paid = 'yes' WHERE id = '$id'");
echo "Setting paid statesucessfull.";
}
else{
echo "paid_state not set";
}
?>
i am guessing it needs to be pulled from the db, and put all those $id in another array var, but i can;t figure out how, and then loop over that array .. can someone point me in the right direction ?

Update checkbox data in SQL-database with PHP

As the title reveals I got an issue with how to update a checkbox that already has data in my SQL database.
My code looks like following:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="inputAll" value="checked" <?php echo $hemsida['All']; ?>/>Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
<?php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$_POST[inputALL]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
?>
The echo $hemsida['Namn'],['Comment'], and ['All'] just brings up and shows the old data thats in the database, but I do not understand what to do to update the checkbox. I have looked everywhere but I am stuck. Thank you in advance!
If I understand your question correctly, you are looking for a way to have a checkbox be either checked or not checked depending on database info. If so, I would try something like this. At the top of your code where you get your database info, put
if($conditionForCheck){
$inputAll = ' checked="checked"';
}
Then in your form
<input type="checkbox" name="inputAll"<?php echo $inputAll; ?> />
your question is not clear but i think you have a column in your database named "all" ? and perhaps this column can take only 1 value (true or false) !!
then you can test this value in your form, if the value is true : checkbox will be checked, else : checkbox will not be checked :
<input type="checkbox" name="inputAll" checked="<?php if($hemsida['All'] == true) echo checked; ?>" />Alla
dont use value="", use checked instead, then test value of $hemsida['All'] if it's true echo checked else anything to do
for your php code and server side of your application you can just test if checkbox is checked and then you have choice for what do you want to assign to your column in database, for example if checkbox is checked create a variable (for example $value_of_checkbox) and assign a value ("true" for exampel) to this variable, then include this variable in your sql code for update database column :
if (isset($_POST['inputALL'])) {
$value_of_checkbox = true;
}
else {
$value_of_checkbox = false;
}
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$value_of_checkbox' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php")
note : i change also sql code in this part : ALL='$value_of_checkbox'

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