Reputation: 307
I have 4 checkboxes and suppose I check only two of them i.e user1 and user3
<input type="checkbox" name="usersp[]" value="1" checked="checked" />User1 <br>
<input type="checkbox" name="usersp[]" value="2"/>User2 <br>
<input type="checkbox" name="usersp[]" value="3" checked="checked" />User3 <br>
<input type="checkbox" name="usersp[]" value="4"/>User4 <br>
and I have two fields ("User_id","Selected") in my database, below is my insert code
foreach($_POST['usersp'] as $user_id) {
$sql = "INSERT INTO sel_users(`User_id`,`Selected`) VALUES('$user_id','')";
$result = mysql_query($sql);
}
My question is how would I insert checked user as value of 1 and other as value of 0 in "Selected" field?
Upvotes: 0
Views: 1700
Reputation: 1
Work for me, save both check and uncheck value to mysql
HTML:
<form method="post" action="jawab25.php">
<input type="hidden" name="check_lista[]" value="<?php echo $r["id"]?>">
<input type="checkbox" name="check_listb[]" value="<?php echo $r["id"]?>">
<input value="Check" type="Submit">
jawab25.php :
foreach($_POST['check_lista'] as $itema){
$string="update trans set ck='N' where id='$itema'";
$tampil=mysql_query($string);
}
foreach($_POST['check_listb'] as $itemb){
$string="update trans set ck='Y' where id='$itemb'";
$tampil=mysql_query($string);
}
Upvotes: 0
Reputation: 1147
Checkbox
gives the value of the Checked
elements under the same name.
So in the example you have given You will not get the unchecked values.
But if you give each checkbox a different name then you can achieve what you want like :-
<input type="checkbox" name="usersp1" value="1" checked="checked" />User1 <br>
<input type="checkbox" name="usersp2" value="2" />User2 <br>
<input type="checkbox" name="usersp3" value="3" checked="checked" />User3 <br>
<input type="checkbox" name="usersp4" value="4" />User4 <br>
And On PHP side
for($i=1;$i<5;$i++){
if(isset($_POST['usersp'.$i]){
$sql = "INSERT INTO sel_users(`User_id`,`Selected`) VALUES('$user_id','1')";
$result = mysql_query($sql);
}
else{
//Somehow you still have to get other User_id's in another way.
}
}
Now you haw to get unselected user ID form somewhere else which you can put into the else condition. You can use Ajax + JS or JQuery for form submission instead of default HTML one so that you can prvice the unchecked values under another name and process them in PHP accordingly
Upvotes: 1
Reputation: 1125
You can't know the (unselected checkboxes) unless you are already know them
Here the only way that can make your dream real
//YOUR USERS IDS
$userIDS=array('1','2','3','4');
//CHECKED USERS ARRAY
$checkedUsers=array('1','3');
if(!isset($_POST['usersp'])){
foreach($userIDS as $userID){
//IF IT IN CHECK ARRAY, CHECK IT!
$checked=(in_array($userID,$checkedUsers))?' checked="checked"':'';
echo '<input type="checkbox" name="usersp[]" value="'.$userID.'"'.$checked.' />';
}
}
else{
foreach($userIDS as $userID){
//USER SELECTED OR NOT?
$selected=(in_array($userID,$_POST['usersp']))?'1':'0';
$sql = "INSERT INTO sel_users(`User_id`,`Selected`) VALUES('$user_id','$selected')";
$result = mysql_query($sql);
}
}
Upvotes: 1
Reputation: 30488
You will get the value of checked checkbox, and not get value of not checked checkbox.
use this tutorials
http://www.html-form-guide.com/php-form/php-form-checkbox.html
determine whether checkbox is checked php $_GET
Upvotes: 1