Jump to content

Passing multiple IDs with Checkbox


janggu

Recommended Posts

I have a list of client on a page and need to add checkboxes on each client. My intention is when users click the checkboxes, the page passes ids of the clients so that I could update the client info – let’s say the checkbox is for getting newsletter. The main purpose of this feature is users can simply click the checkboxes and update info without going into each client’s detailed page.

Could anyone please tell me some directions with an example?

Thanks a lot!
Link to comment
Share on other sites

I won't provide you with an example, because I don't have a real simple one, but I'll give you a few tips.

1) Keep in mind that only the checked boxes will be sent on to the processing page. This gives you the option of setting the value to the client id and you do the same action to all the values that are submitted.
2) You can name the checkboxes in 1 of 2 ways. Use an array (ie. ckClientID[]) or you can simply name them with a similar naming convention (ie. ck1234, where 1234 is the client id), so when you submit the form, you loop through all the results and look for variables that start with "ck" and handle them accordingly.
Link to comment
Share on other sites

Ok. I lied. Here is what I have so far.

client.php page
[code]
<form action="update.php" method="post" enctype="multipart/form-data">
          <table width="100%">               
            <tr>            
              <th valign="bottom" align="center" nowrap>Date</th>
              <th valign="bottom" align="center" nowrap>Name</th>
              <th valign="bottom" align="center" nowrap>Email</th>
              <th valign="bottom" align="center" nowrap>Subscribe</th>
              <th valign="bottom" align="center" nowrap>Action</th>              
            </tr>            
            
             <?php
              
              if (mysql_num_rows($getClient) <> 0) {
              
                  // display
                  while ($r = mysql_fetch_array($getClient)) {
                      echo "<tr>";                    
                    echo "<td align='center' valign='top'>" .$r['date']. "</td>";
                    echo "<td align='center' valign='top'>" .$r['name']."</td>";
                    echo "<td align='center' valign='top'>" .$r['email']. "</td>";
                    echo "<td align='center' valign='top'><input type='checkbox' name='id[]' value=".$r['id']."></td>";                    
                    echo "<td valign='top' align='center'><a href='view.php?id=".$r["id"]."'>View</a>";                        
                    echo "</td>";
                    echo "</tr>";
                    echo "<tr><td colspan='5'><hr size='1' noshade></td></tr>";
                }
                }
                    
              ?>        
           <tr>
               <td align="center" colspan="5">
           <input type="submit" value="Update">
               </td>
           </tr>      
         </table>
         </form>
[/code]

update.php page

[code]
if (isset($_POST['id']))
{
$counter = 1;
while ($counter < 1) {
if ($_POST[$id] != "") {
$id = "id" . $counter;

$sql = "UPDATE client2 SET
FA = '1'
WHERE id='$id'
";
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
}
$counter++;
}
}
[/code]
Link to comment
Share on other sites

That wasn't so hard, was it?

Your while loop is never going to go anywhere. You set it to 1 and then immediately check to see if it's less than 1.

Better:[code]
if (isset($_POST['id']))
{
    foreach($_REQUEST['id'] as $val)
   {
    $sql = "UPDATE client2 SET
    FA = '1'
    WHERE id='$val'
    ";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
   }
}
[/code]
Link to comment
Share on other sites

[!--quoteo(post=373023:date=May 10 2006, 06:13 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 10 2006, 06:13 PM) [snapback]373023[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That wasn't so hard, was it?

Your while loop is never going to go anywhere. You set it to 1 and then immediately check to see if it's less than 1.

Better:[code]
if (isset($_POST['id']))
{
    foreach($_REQUEST['id'] as $val)
   {
    $sql = "UPDATE client2 SET
    FA = '1'
    WHERE id='$val'
    ";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
   }
}
[/code]
[/quote]

It works great. Thanks a lot!!!

There is another issue though... I unchecked the boxes and saved it with the following code but it didn't work.
[code]
if (isset($_POST['id']))
{
    foreach($_REQUEST['id'] as $val)
   {
    $sql = "UPDATE client2 SET
    FA = '1'
    WHERE id='$val'
    ";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
   }
}
else
{
    foreach($_REQUEST['id'] as $val)
   {
    $sql = "UPDATE client2 SET
    FA = '0'
    WHERE id='$val'
    ";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
    }
}
[/code]
Link to comment
Share on other sites

unchecked boxes are not going to be passed on. in other words, ONLY the values of checked boxes are going to be available. What you should do here, is to reset the values to zero and then update them one by one.
[code]if (isset($_POST['id']))
{
    ## Reset the values
    $sql = "UPDATE client2 SET FA='0'";
    mysql_query($sql);

    ## Process the check boxes
    foreach($_REQUEST['id'] as $val)
    {
    $sql = "UPDATE client2 SET
    FA = '1'
    WHERE id='$val'
    ";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
   }
}[/code]
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.