Jump to content

Keeping checkbox array checked after a post


valency

Recommended Posts

I can use some help in figuring out what I am doing wrong here or need to do.

I am populating an array of checkboxes from the database of userID's. Any checkbox can be checked all even all based on the javascript code that I wrote for that, now I want the checkbox that are checked remain checked after the post which updates the database in another column. Here is the code so far:

 

<? echo "<form action=\" \"method=\"POST\" name=\"theForm\"><table border=2>";

    echo "<tr><th>CheckBox</th><th>Username</th><th>Password</th><th>First name</th><th>Last name</th><th>Email</th></tr>";

 

    if ($tr > 0) {

        for ($cc = 0; $cc<$tr; $cc++)  {

          $row = $db->results[$cc];

 

            echo "<tr><td><input type=\"checkbox\" name=\"id[]\" value=\"".$row->userID."\"><td> ".$row->username."</td><td> ".$row->password." </td><td> ".$row->firstName." </td><td>".$row->lastName." </td><td> ".$row->email." </td></tr>\n";

                }

          }

 

  echo "</table><input type=\"submit\" value=\"Click here to delete the selected accounts \"></form>";

    ?>

 

 

So farthis code does exactly what i Want it to do, now I want to keep the checkbox checked for each userID value boxchecked after a post or submit is set. I have tried lotsof methods including it inside the loop functions,but all wont keep the check box checked...Please help out especially in regards to this code since all other blogs were not this code specific.

 

 

 

Link to comment
Share on other sites

I steal your code and rewrite on my own text editor. Try these out. Hope you understand the code.

for ($cc = 0; $cc<$tr; $cc++)  {
$row = $db->results[$cc];
$check_value = "";

if($_SERVER['REQUEST_METHOD'] == "POST"){
	// So the form is posted!
	if(isset($_POST['id']) && sizeof($_POST['id']){
		foreach($_POST['id'] as $val){
			if($val == $row->userID)
			{
				// Ahh I found the checked value!
				$check_value = "checked=\"checked\"";
			}
		}
	}
}

echo "<tr>
		<td>
			<input type=\"checkbox\" $check_value name=\"id[]\" value=\"".$row->userID."\"><td> ".$row->username."
		</td>
		<td> ".$row->password." </td>
		<td> ".$row->firstName." </td>
		<td>".$row->lastName." </td>
		<td> ".$row->email." </td>
	</tr>\n";
}

Link to comment
Share on other sites

Hey Xeno, the code worked right, the checked box stayed checked but when the update first occurs with the checked value, when u uncheck the value, it does not update after resubmitting. Do I need to include another if in the Update value or what? The code below is a continuation from the previously posted problem with checkboxes!

<?

 

//if  the data gets posted, set the ID row to the checkboxes

if (isset($_POST['id']))

{

$checkboxofid = sizeof($_POST['id']);

//count the variables

    $updated = 0;

$inserted = 0;

    for ($ii=0;$ii<$checkboxofid; $ii++)

{

$queryupdate = 'UPDATE users SET excludeFromD = 1 WHERE userID = '.$_POST['id'][$ii];

$result3 = $db->query($queryupdate);

//echo $queryupdate, "<br>";

$updated++;

}

 

    echo "<BR />".$updated." account(s) updated (excluded from designs only)<br /><br />";

}

?>

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.