tomtimms Posted May 7, 2010 Share Posted May 7, 2010 I have an edit form that I can't seem to get the values from when submitted. If the first checkbox is checked I can get and display all the values. However if anything other than the first box is checked I just get the checkbox value. Here is my form. <input type="checkbox" name="cbox[]" value="<?PHP echo $rows['id'];?>" /> <input type="text" name="adjustment[]" size="5" /> here is my process page $id = $_POST['cbox']; $adjustment = $_POST['adjustment']; // loop through array $numb= count($id); $number = $numb-1; for ($i=0; $i<=$number; $i++) { $itno = $id[$i]; $desc = $adjustment[$i]; if ($id[$i] <> '') { echo "Item: " . $itno . " Description: " . $desc . "<p>"; } } I am trying to pass the values from the form from only the boxes that are checked. Anyone know what I did wrong? Link to comment https://forums.phpfreaks.com/topic/201017-form-array-show-values/ Share on other sites More sharing options...
bobby317 Posted May 7, 2010 Share Posted May 7, 2010 Could you post the entire form and php script. I am not sure why you named your first check box cbox[] But one thing I notice is that your $id variable you have $id = $_POST['cbox']; I think it sould be $id = $_POST['cbox[]']; But post the whole script and I will try my best to help. Link to comment https://forums.phpfreaks.com/topic/201017-form-array-show-values/#findComment-1054677 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2010 Share Posted May 7, 2010 The way HTML handles checkbox arrays is that only those items that are checked get sent to the processing script. Put <?php if (isset($_POST['cbox'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; } ?> at the start of your processing script to see what is being sent. See if my test script helps you: <?php if (isset($_POST['submit'])) { echo '<pre>' .print_r($_POST,true) . '</pre>'; if (isset($_POST['cbox'])) { foreach ($_POST['cbox'] as $i => $item) { echo $i . ': ' . $item . ' -- Adjustment: ' . $_POST['adjustment'][$i] . "<br>\n"; } } } ?> <html> <head> <title>Testing</title> </head> <body> <form action="" method="post"> <?php $num = rand(5,10); for ($i=0;$i<$num;++$i) { echo $i . ': <input type="checkbox" name="cbox[' . $i . ']" value="' . rand(100,200) . '">, Adjustment: <input type="text" name="adjustment[' . $i . ']" size="5"><br>' . "\n"; } ?> <input name="submit" value="Send Values" type="submit"> </form> </body> </html> Ken Link to comment https://forums.phpfreaks.com/topic/201017-form-array-show-values/#findComment-1054681 Share on other sites More sharing options...
tomtimms Posted May 7, 2010 Author Share Posted May 7, 2010 Ok so far I have this as my Form Code <html> <head> <title>Testing</title> </head> <body> <form action="processing.php" method="post"> <?php while ($rows = mysql_fetch_assoc($result)){ echo '<input type="checkbox" name="cbox[]" value="' . $rows['company'] . '"> Adjustment: <input type="text" name="adjustment[]" size="5"> Company: '.$rows['name'].' <br>' . "\n"; } ?> <input name="submit" value="Send Values" type="submit"> </form> </body> </html> and this as my processing if (isset($_POST['submit'])) { if (isset($_POST['cbox'])) { foreach ($_POST['cbox'] as $i => $item) { $sql = "UPDATE revenue_company SET status = '1' ,adjustment = ".mysql_real_escape_string($_POST['adjustment'][$i])." WHERE company = ".$item.""; $result = $db->db_query($sql); echo $sql; } } } My form display's 3 rows of data. If you check the second row or third row that adjustment field doesn't process. However if you select all 3 rows it works just fine. Anyone know what's going on? Link to comment https://forums.phpfreaks.com/topic/201017-form-array-show-values/#findComment-1054799 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2010 Share Posted May 7, 2010 If you notice is my sample code, I use an explicit index when creating the input checkbox arrays, without it the arrays sent to your script won't match up correctly. Change your code to be: <?php $i = 0; while ($rows = mysql_fetch_assoc($result)){ echo '<input type="checkbox" name="cbox[' . $i . ']" value="' . $rows['company'] . '"> Adjustment: <input type="text" name="adjustment[' . $i . ']" size="5"> Company: '.$rows['name'].' <br>' . "\n"; $i++; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/201017-form-array-show-values/#findComment-1054810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.