Jump to content

php and checkboxes


sw45acp

Recommended Posts

Hi, I have a checklist that is generated from a server that allows users to check stuff off as they get them done. Because each user will have a different number of checkboxes, I use an array in the checkbox name like we are all supposed to do and put the id of that item inside the brackets when its generated from the server, like this:

<input type="checkbox" name="item[9]" value="1" />

1 stands for the item being complete, 0 stands for the item being incomplete

And I process it like this:

$item_list = $_POST['item'];
foreach($item_list as $id => $item) {
    $q = mysql_query("UPDATE completion SET `complete` = '$item' WHERE `id` = '$id'");
}

 

The problem here is that when someone decides that an item is not complete and unchecks it, the loop skips over it, and it still stays as complete. That is what I'm trying to fix.

 

Thank you for any help

 

 

Link to comment
Share on other sites

while that is true, what do I do if the item is already checked as complete when it is written to the page.  I have it set so that if it is already complete it is checked off. If the person decides its not complete, and unchecks it, php will not recognize a checkbox that is unchecked so it will skip over it

Link to comment
Share on other sites

Let me show you an example of what I mean. From the new grand theft auto IV video game, there is a checklist to complete stuff (missions, etc).

 

http://www.gta4.tv/100percent.php

 

This website has it set up so users can check off what they have done. What if they accidently checked off one by mistake? So naturally you would uncheck it because it is not complete and resubmit, but php does not recognize checkboxes that are not checked. Sorry I'm really bad at wording things.

Link to comment
Share on other sites

i will give you outline but cant give exact code due to lack of programing in structured php as i use cakephp

 

 

$result = mysql_query("select * from completion");

$item_list = $_POST['item'];

 

//then loop on its result like

 

for(.......){

 

      // get id here of record

 

if(in_array($id,$item_list) ){

//its 1 here

}else{

//its 0 here

}

 

 

}

Link to comment
Share on other sites

I just wanted to reply to you again and say thank you for your help. You're outline pointed me in the correct direction and yes I had to use array_key_exists() instead of in_array(). It is very annoying that unchecked checkboxes are not sent to the php script, and because they are all dynamically generated, making a hidden field for each one would be impossible. This was my finished code, and it works great!

 

$item_id = $_POST['item_id'];
    $q = mysql_query("SELECT * FROM table");
    //create an array to hold keys (id of each checklist item) from database
    $array_keys = array();
    while($rows = mysql_fetch_array($q)) {
        /*make it an associative array, filling it with the id of the item as its key, and the value as blank*/
        $array_keys[$rows['id']] = '';
    }
    //now its time to do the comparison with the array from the database and from the form
    foreach ($array_keys as $key => $value) {
        if (array_key_exists($key,$item_id)) {
            /*check if the key exists from the database in the form array 'haystack'
            if it does, and its id and a value of 1 (meaning its complete), to a new array,
            if not, and its id and a value of 0*/
            $new_array[$key] = '1';
        }else{
            $new_array[$key] = '0';
        }
    }
    //print_r($new_array);
    foreach($new_array as $id=>$complete) {
        //here, for every checkbox, update database
    }

Link to comment
Share on other sites

What you're complaining about is the design of the checkbox form element in HTML.  Checkboxes only get sent when checked and have no associated value.  I didn't check this, but chances are the browser doesn't even send them to the server.

Link to comment
Share on other sites

Here am having similar, can u help me out. below is my code

 


<?

   //  $i = 1;
   //  echo "<tr style=\"height: 30px;\"><td valign=\"bottom\" style=\"border-bottom: 1px solid black;\">New Client</td><td valign=\"bottom\" style=\"border-bottom: 1px solid black;\"><input   type=\"checkbox\" name=\"".intval($i)."\"  id=\"".intval($i)."\"></td></tr>";
   //  $i++;

// print_r($data);



     for($i = 0; $i < count($arr); $i++)
     {

   //    if ($i == 5)  echo '<tr style="height: 80px;"><td valign="middle" align="left"><b><u>Campaign Setup:</u></b></td></tr>';

       if ($i == count($arr) -  echo '<tr style="height: 60px;"><td valign="bottom" align="left"><b><u>Completion:</u></b></td></tr>';

    //   if ($i ==  echo '<tr ><td style="height: 60px;" valign="bottom"><br>Check to see if the client already has listings at the following sites and if they don\'t, create them. Make sure all URLs link back to client\'s website:<br><br></td><td></td></tr>';

       if ($i >=8 && $i <= 10) $borderSize = 0;
                      else    $borderSize = 1;
       $text = $arr[$i];

       if ($text[0] == "@") {

       							 $text = substr($text, 1);

       							 if ($data["code"][intval($i)] == 1) { $checkedY = " checked "; $checkedN = "checked"; $script = $script."\nYesOrNo(Y".intval($i)."); "; }

       							 else { $checkedN = " checked "; $checkedY = "checked"; }




       							 $control = '<table border="0">

       							 				<tr>

       							 					<td valign="bottom">

       							 						Yes <input type="checkbox" '.$checkedY.' name="'.intval($i).'" id="Y'.intval($i).'" value="1" onclick="YesOrNo(this);">	</td>

       		<td style="min-width: 10px;"><div id="none"></div></td>

       			<td valign="bottom">				 						No <input '.$checkedN.'  type="checkbox" name="'.intval($i).'" id="N'.intval($i).'" value="0" onclick="YesOrNo(this);">


       							 					</td>

       							 				</tr>

       							 			</table>';



       						} else {
       						$control = "<input onclick=\"checkCondition();\"  type=\"checkbox\" value=\"1\" name=\"".intval($i)."\"  id=\"".intval($i)."\" ";


       						//echo intval($data["code"][intval($i)]);

       						if ($data["code"][intval($i)] == "1") $control=$control." checked ";

       						$control = $control.">";
       						}

       echo "<tr id=\"tr_".intval($i)."\" style=\"height: 50px;\"><td valign=\"bottom\" style=\"border-bottom: ".$borderSize."px solid black;\">".$text."</td><td valign=\"bottom\" style=\"border-bottom: ".$borderSize."px solid black;\">$control</td></tr>";

     } ?>



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.