art15 Posted May 22, 2008 Share Posted May 22, 2008 Hi, I have a list of products and a check box next to it. The checkbox reads as <input type="checkbox" name="sale<?php print '$rownum'?>" value="1" />. The $rownum is having value 1,2, so on. now, If i select one of the checkbox and hit submit it will update that record in the databse. however if I select 2 or 3 checkbox and hit submit it just updates one record not all the 3 which I have selected. Can anyone tell what's wrong. With $_post i am using $sale=$_POST["sale".'$rownum']; Is there something which is wrong? thanks Quote Link to comment https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/ Share on other sites More sharing options...
jonsjava Posted May 22, 2008 Share Posted May 22, 2008 here's a file that I figured out a fix (it's in an opensource mailgroup solution I put out). Parse through it, and it should help you figure it out. show contacts: <?php elseif($_GET['page'] == "show_all_contacts"){ ?><form action="remove_contacts.php" method="POST" name="removeContacts"> <?php $contact_count = 1; $checkbox_count = 0; $sql = "SELECT email_address, id, name from {$_SESSION['username']}_table_{$list_name} WHERE verified =1 ORDER BY email_address"; $result = mysql_query($sql); print "<h1><strong>VIEW/REMOVE CONTACTS</strong></h1><br /><table align='center'>\n<tr>\n"; while ($row = mysql_fetch_assoc($result)){ if ($contact_count == 3){ print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n</tr>\n"; $contact_count = 1; } else{ print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n"; ++$contact_count; } } if ($contact_count == 1){ print "</tr>\n";} print "</table>\n"; $contact_count = 1; $sql = "SELECT email_address, id, name from {$_SESSION['username']}_table_{$list_name} WHERE verified =0 ORDER BY email_address"; $result = mysql_query($sql); print "<h1><strong>VIEW/REMOVE UNVERIFIED CONTACTS</strong></h1><br /><table align='center'>\n<tr>\n"; while ($row = mysql_fetch_assoc($result)){ if ($contact_count == 3){ print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n</tr>\n"; $contact_count = 1; } else{ print "<td align='left'><input type='checkbox' name='{$row['id']}' value='".$row['id']."'><a href='remove_contacts.php?id={$row['id']}'><img src='templates/blue/recycler.gif' alt='Remove' title='Remove' border='0'></a>".$row['name']."(".$row['email_address'].")</td>\n"; ++$contact_count; } } if ($contact_count == 1){ print "</tr>\n";} print "<tr>\n<td align='left'><input type=submit value='DELETE'></td>\n</tr>\n</table>\n</form>\n"; } ?> remove contacts <?php if (isset($_GET['id'])){ $removal_sql = "DELETE FROM `{$_SESSION['username']}_table_{$list_name}` WHERE id={$_GET['id']} LIMIT 1;"; mysql_query($removal_sql); } else{ $removal = implode($_POST); foreach($_POST as $key => $value) { if ($value != "DELETE"){ $removal_sql = "DELETE FROM `{$_SESSION['username']}_table_{$list_name}` WHERE id={$value} LIMIT 1;"; mysql_query($removal_sql); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/#findComment-547755 Share on other sites More sharing options...
.josh Posted May 23, 2008 Share Posted May 23, 2008 You need to update the database (run the query) for each checkbox value that's posted. Most people do this by passing the checkbox data in an array instead of an individual variable for each one, and then either loop through the array (putting the query inside the loop) or rephrasing the query (it really depends on the data etc.. as to what's best). Quote Link to comment https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/#findComment-547763 Share on other sites More sharing options...
art15 Posted May 23, 2008 Author Share Posted May 23, 2008 Hi, Can you help me with the loop which needs to run for each post? I tried the following and gets an error message $sale=$_POST["sale".'$rownum']; foreach($sale as $key => $value) { echo $value; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/#findComment-547783 Share on other sites More sharing options...
art15 Posted May 23, 2008 Author Share Posted May 23, 2008 Hi, Can anyone help me with this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/#findComment-547896 Share on other sites More sharing options...
.josh Posted May 23, 2008 Share Posted May 23, 2008 Hey art, Using an array to pass multiple values from a form is a very common question that not only comes up a lot, but you will also find it inside people's code snippets in practically every other thread...because like 99% of forms have multiple values to be passed, and it's just more logical to use an array instead of individual variables. I pointed you in the right direction with my other post. This post is me suggesting you use the search function for a very common coding practice. Good luck and happy coding Quote Link to comment https://forums.phpfreaks.com/topic/106852-post-the-checkbox-value/#findComment-548074 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.