Jump to content


Photo

Passing multiple IDs with Checkbox


  • Please log in to reply
7 replies to this topic

#1 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 10 May 2006 - 04:41 PM

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!


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 May 2006 - 04:55 PM

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.

Info: PHP Manual


#3 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 10 May 2006 - 05:21 PM

Thanks for your tips but my PHP experience is not enough to come up with my own code.

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 May 2006 - 05:41 PM

I suggest you at least make an attempt and then post what you have. No one here is just going to write it for you.

Info: PHP Manual


#5 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 10 May 2006 - 06:08 PM

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

client.php page
<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>

update.php page

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


#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 May 2006 - 06:13 PM

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

Info: PHP Manual


#7 janggu

janggu
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 10 May 2006 - 06:52 PM

[!--quoteo(post=373023:date=May 10 2006, 06:13 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ May 10 2006, 06:13 PM) View Post[/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:
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());
   }
}
[/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.
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());
    }
}


#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 10 May 2006 - 07:04 PM

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

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users