anser316 Posted April 12, 2008 Share Posted April 12, 2008 Hi I am having a couple of problems with my checkboxes. The first is that the id number, which i have put as the value of the checkbox, does post properly, but the others post only in order of the list, not of what has been checked. e.g. ID BID Dname stock level 1 1 a 3 3 2 2 b 4 4 3 3 c 5 5 If i check 2 and 3 ID BID Dname stock level 2 1 a 3 3 3 2 b 4 4 So the checkbox for the ID works but the other values are not taken from that ID. I am sure one of the factors is on my second form with where i have put my if statements and while loops, and where i have opened and closed them. $result =mysql_query("SELECT..... echo "<b>Minimum Stock Level Met - Reorder Needed</b><p>"; echo "<form action= conorder.php method=POST>"; echo "<table border='1'>"; echo "<tr> <th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th></tr>"; while($row = mysql_fetch_array( $result2 )) { echo "<tr><td>"; echo "<input type='hidden' name=drug_id[] value='$row[drug_id]'>"; echo $row['drug_id']; echo "</td><td>"; echo "<input type='hidden' name=branch_id[] value='$row[branch_id]'>"; echo $row['branch_id']; echo "</td><td>"; echo "<input type='hidden' name=drug_name[] value='$row[drug_name]'>"; echo $row['drug_name']; echo "</td><td>"; echo "<input type='hidden' name=total_stock[] value='$row[total_stock]'>"; echo $row['total_stock']; echo "</td><td>"; echo "<input type='hidden' name=reorder_level[] value='$row[reorder_level]'>"; echo $row['reorder_level']; echo "</td><td>"; echo "<input type=checkbox name=ticked[] value='{$row['drug_id']}'>"; echo "</td></tr>"; } echo "</table>"; echo "<input type =submit value= Submit>"; echo "<input type = reset>"; echo "</form>"; if (isset($_POST['ticked'])) { for ($i=0; $i<count($_POST['ticked']); $i++) { $result =mysql_query("UPDATE branch_items SET status ='ORDERED' WHERE drug_id=".$_POST['ticked'][$i]." AND branch_id=".$_POST[branch_id][$i].""); } if (isset($_POST['ticked'])) { for ($i=0; $i<count($_POST['ticked']); $i++) { $result2 =mysql_query("SELECT status from branch_items WHERE drug_id=".$_POST['ticked'][$i]." AND branch_id=".$_POST[branch_id][$i].""); }} $row = mysql_fetch_array( $result2 ); echo "<table border='1'>"; echo "<tr><th>DRUG ID</th> <th>Branch ID</th><th>DRUG NAME</th><th>STOCK</th><th>REORDER LEVEL</th><th>Status</th></tr>"; for ($i=0; $i<count($_POST['ticked']); $i++) {//as $drug_id){ echo "<tr><td>"; echo $_POST['ticked'][$i]; echo "</td><td>"; echo $_POST[branch_id][$i]; echo "</td><td>"; echo $_POST[drug_name][$i]; echo "</td><td>"; echo $_POST[total_stock][$i]; echo "</td><td>"; echo $_POST[reorder_level][$i]; echo "</td><td>"; echo $row[status]; echo "</td></tr>"; } } Quote Link to comment Share on other sites More sharing options...
amites Posted April 12, 2008 Share Posted April 12, 2008 my brain might be a little mushy but I'm having a hard time figuring out what you want help with specifically, you're on the right track, providing examples and code, but a specific question makes things go much more smoothly, not to mention helps train your brain to debug your own work Quote Link to comment Share on other sites More sharing options...
laffin Posted April 12, 2008 Share Posted April 12, 2008 Its cuz only checked boxes get sent. u didnt check 1, so boxes 2 & 3 got sent however all hidden fields get sent. so add keys to the indexes. $counter=0; while($row = mysql_fetch_array( $result2 )) { echo "<tr><td>"; echo "<input type='hidden' name=drug_id[$counter] value='$row[drug_id]'>"; . . echo "<input type=checkbox name=ticked[] value='$counter'>"; echo "</td></tr>"; $counter++; } now each array value in ticked returns an index value associated to the info Quote Link to comment Share on other sites More sharing options...
anser316 Posted April 12, 2008 Author Share Posted April 12, 2008 ok ive tried that, and it makes sense, thanks, but when i put this in if (isset($_POST['ticked'])) { for ($i=0; $i<count($_POST['ticked']); $i++) { echo "<br>drug id: "; echo $_POST[drug_id][$i]; echo " branch id: "; echo $_POST[branch_id][$i]; echo " exp: "; echo $_POST[drug_name][$i]; }} i still get the checkbox 1 not the one i selected. i dont think im setting $i to equal the value in $counter or its not storing properly. can some1 please help me with this Quote Link to comment Share on other sites More sharing options...
anser316 Posted April 12, 2008 Author Share Posted April 12, 2008 hi can someone help please, it will solve alot of problems thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted April 12, 2008 Share Posted April 12, 2008 Forget all the hidden fields. List the data with c/box an each row name='cbox[]' value='id' On the next page, to list the selected items, requery the database <?php $items = join (',', $_POST['cbox']); $sql = "SELECT * FROM tablename WHERE id IN ($items)"; // query and list selected data ?> Quote Link to comment Share on other sites More sharing options...
anser316 Posted April 12, 2008 Author Share Posted April 12, 2008 sorry i do not follow, could you perhaps elaborate on the first form Quote Link to comment Share on other sites More sharing options...
doni49 Posted April 13, 2008 Share Posted April 13, 2008 <input type="checkbox" name="cbox[bx1]"> <input type="checkbox" name="cbox[bx2]"> <input type="checkbox" name="cbox[bx3]"> <input type="checkbox" name="cbox[bx4]"> <input type="checkbox" name="cbox[bx5]"> <input type="checkbox" name="cbox[bx6]"> $_POST['cbox'] will be an array listing each checkbox that was selected. If a box isn't selected, it won't be part of the array. Quote Link to comment Share on other sites More sharing options...
laffin Posted April 13, 2008 Share Posted April 13, 2008 ok ive tried that, and it makes sense, thanks, but when i put this in if (isset($_POST['ticked'])) { for ($i=0; $i<count($_POST['ticked']); $i++) { echo "<br>drug id: "; echo $_POST[drug_id][$i]; echo " branch id: "; echo $_POST[branch_id][$i]; echo " exp: "; echo $_POST[drug_name][$i]; }} i still get the checkbox 1 not the one i selected. i dont think im setting $i to equal the value in $counter or its not storing properly. can some1 please help me with this cuz the keys for ticked are not sequential. using foreach will solve yer problem. if (isset($_POST['ticked'])) { foreach ($_POST['ticked'] as $id) { echo "<br>drug id: "; echo $_POST[drug_id[$id]]; echo " branch id: "; echo $_POST[branch_id[$id]]; echo " exp: "; echo $_POST[drug_name[$id]]; } Quote Link to comment 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.