0

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?

5
  • 2
    If your table sel_users is for selected users only, why do you need to insert users that haven't been selected? Surely, only inserting the selected users is the best course of action? Commented Sep 21, 2012 at 11:48
  • 1
    Instead of <input type="checkbox" name="usersp[]" value="1" />, use <input type="hidden" name="usersp[1]" value="0" /><input type="checkbox" name="usersp[1]" value="1" />. Commented Sep 21, 2012 at 11:51
  • The problem is when I use my update query, suppose I update these checkboxes and uncheck user1,user3 and check user2 and user4, then how would I make update query, because it also update and insert at the same time. Commented Sep 21, 2012 at 11:51
  • @DCoder Actually I am fetching all these users from database but I am trying through for loop. Commented Sep 21, 2012 at 11:54
  • That doesn't change the logic - generate a pair of inputs for each user ID, using that ID as the index in the input name... Commented Sep 21, 2012 at 11:55

4 Answers 4

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, I am using in my way, but it helps me. Thanks again :)
1

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

Comments

1

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

Comments

0

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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.