Jump to content

[SOLVED] Help with checkbox data


kryppienation

Recommended Posts

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.