janggu Posted May 10, 2006 Share Posted May 10, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/ Share on other sites More sharing options...
ober Posted May 10, 2006 Share Posted May 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-34967 Share on other sites More sharing options...
janggu Posted May 10, 2006 Author Share Posted May 10, 2006 Thanks for your tips but my PHP experience is not enough to come up with my own code. Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-34974 Share on other sites More sharing options...
ober Posted May 10, 2006 Share Posted May 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-34986 Share on other sites More sharing options...
janggu Posted May 10, 2006 Author Share Posted May 10, 2006 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 SETFA = '1'WHERE id='$id'";$result = mysql_query($sql) or die("Invalid query: " . mysql_error());} $counter++; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-35006 Share on other sites More sharing options...
ober Posted May 10, 2006 Share Posted May 10, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-35010 Share on other sites More sharing options...
janggu Posted May 10, 2006 Author Share Posted May 10, 2006 [!--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] Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-35032 Share on other sites More sharing options...
ryanlwh Posted May 10, 2006 Share Posted May 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/9481-passing-multiple-ids-with-checkbox/#findComment-35038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.