kryppienation Posted November 27, 2008 Share Posted November 27, 2008 I am having trouble with the way i am getting data from checkboxes on a form. I have a form and it has several checkboxes. A user then clicks the desired checkboxes and clicks send. Now the problem i am having is that it is only giving me the last checkbox that was checked. I need the value from each checkbox that was checked. I have supplied the code as well as visual of each. ----------- form $totalvalue = 0; echo '<table border="1">'; echo '<form action="fishsellfinal.php" method="post">'; echo '<tr><td align="center"><font color="blue">Sell?</td><td align="center"><font color="blue">invid</td><td align="center"><font color="blue">iid</td><td align="center"><font color="blue">item</td><td align="center"><font color="blue">dbvalue</td><td align="center"><font color="blue">value</td></tr>'; while ($row = mysql_fetch_assoc($pullfishresult)) { echo '<tr><td align="center"><input type="checkbox" name="sell?" value="'.$row['invid'].'"></td><td align="center"> '.$row['invid'].'</td><td align="center"> ' . $row['iid'] . '</td><td align="center"> ' . $row['item'] . '</td><td align="center"> ' . $row['dbvalue'] . '</td><td align="center"> ' . $row['worth'] . ' Krupples</td></tr>'; $totalvalue = $totalvalue + $row['worth']; $formattedtotalvalue = number_format($totalvalue, 2); } echo '<tr><td align="center" colspan = "6">Total of all fish: '.$formattedtotalvalue.' Krupples </td></tr>'; echo '<tr><td colspan="6"><p><center><input type="submit" value="Sell" name="sell"></center></td></tr>'; echo '</form>'; echo '</table>'; ----------- ----------- incorrect results if (isset($_POST['sell'])){ echo ''.$_POST["sell?"].'<p>'; }else{ echo 'there is a problem it\'s not reading the form.'; } ----------- Now from the visual's you can see that there are 3 differant items checked, yet on the form submission it only sends the last value... i need to the value's of all checked items... can someone please help me out. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 27, 2008 Share Posted November 27, 2008 You are giving all the checkboxes the same name (is a question mark even a valid character for a field name???). Either give the field a unique name OR give them an array name using brackets; e.g. name="sell[]" Then the checked values will be received as an array. Quote Link to comment Share on other sites More sharing options...
kryppienation Posted November 27, 2008 Author Share Posted November 27, 2008 so if i change the name in the form file.... echo '<tr><td align="center"><input type="checkbox" name="sell[]" value="'.$row['invid'].'"></td><td align="center"> '.$row['invid'].'</td><td align="center"> ' . $row['iid'] . '</td><td align="center"> ' . $row['item'] . '</td><td align="center"> ' . $row['dbvalue'] . '</td><td align="center"> ' . $row['worth'] . ' Krupples</td></tr>'; what do i change the other file to?? See the problem is that there could be any quantity, as many records as there are in the database that meet the query. I am just learning new things here, i don't understand how to make them work sometimes. Quote Link to comment Share on other sites More sharing options...
kryppienation Posted November 27, 2008 Author Share Posted November 27, 2008 Is there anyone who could show me how to do this as an array using the code i have? I have tried to change the name to "sell[]" and it comes up blank. All i am trying to do is bring all of the iid's from the checked boxes to the next page and echo them out so that i can process them. when creating the checkbox names it's important that there can be any number of names depending upon how many times the while statement loops. i'm not sure how to make an array do this. Quote Link to comment Share on other sites More sharing options...
GKWelding Posted November 27, 2008 Share Posted November 27, 2008 You can't call your checkboxes sell and your submit button sell either, just doesn't work like that. Try the following... $totalvalue = 0; echo '<table border="1">'; echo '<form action="fishsellfinal.php" method="post">'; echo '<tr><td align="center"><font color="blue">Sell?</td><td align="center"><font color="blue">invid</td><td align="center"><font color="blue">iid</td><td align="center"><font color="blue">item</td><td align="center"><font color="blue">dbvalue</td><td align="center"><font color="blue">value</td></tr>'; while ($row = mysql_fetch_assoc($pullfishresult)) { echo '<tr><td align="center"><input type="checkbox" name="sell[]" value="'.$row['invid'].'"></td><td align="center"> '.$row['invid'].'</td><td align="center"> ' . $row['iid'] . '</td><td align="center"> ' . $row['item'] . '</td><td align="center"> ' . $row['dbvalue'] . '</td><td align="center"> ' . $row['worth'] . ' Krupples</td></tr>'; $totalvalue = $totalvalue + $row['worth']; $formattedtotalvalue = number_format($totalvalue, 2); } echo '<tr><td align="center" colspan = "6">Total of all fish: '.$formattedtotalvalue.' Krupples </td></tr>'; echo '<tr><td colspan="6"><p><center><input type="submit" value="Sell" name="submit"></center></td></tr>'; echo '</form>'; echo '</table>'; if (isset($_POST['submit'])){ $checkboxes = $_POST['sell']; foreach($checkboxes as $checkbox){ echo $checkbox; } }else{ echo 'there is a problem it's not reading the form.'; } Edited to correct a cockup by me... Quote Link to comment Share on other sites More sharing options...
kryppienation Posted November 27, 2008 Author Share Posted November 27, 2008 awesome making progress, this will print all 3 numbers out... raw. Is there anyway i can make each echo a variable instead? the echo was mainly for me to see if it's working... i will need each number in variable form to actually manipulate it. Quote Link to comment Share on other sites More sharing options...
kryppienation Posted November 27, 2008 Author Share Posted November 27, 2008 Never mind, i figured it out, that worked excellent!!! Thanks a lot for your help. 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.