1
while($row = mysql_fetch_array($result)){       
echo '<tr>
        <td>'.$row[0].'</td>
        <td>'.$row[1].'</td>
        <td>'.$row[2].'</td>';
        $price=$row['price'];
        echo "<td><input type='checkbox' name='er' value='$price'></td>";
echo "</tr>";

PHP file

 <?php
       $ercharge=$_POST['er'];
       echo $ercharge;
?>

I have a list of charges that was from a mysql table and it has a checkbox for each item so it would compute the sum. The above code works and it outputs the price of the checked item. The problem is, it's only one item. When multiple items are checked, only one is outputted.

3
  • Is your all check box having same name? Commented Dec 5, 2012 at 14:04
  • @Tornado well yes since they are in a loop. Commented Dec 5, 2012 at 14:07
  • see my answer may solve your problem Commented Dec 5, 2012 at 14:13

4 Answers 4

1

Try this:-

echo "<td><input type='checkbox' name='er[]' value='$price'></td>";
Sign up to request clarification or add additional context in comments.

3 Comments

This should be the answer. But i think the OP is looking for the sum. Which he can use array_sum( $_POST['er'] ) to get the result
It has an error. It says. Array to string conversion That code is on a form so when it is submitted, it will then be computed.
Doing this means that the 'er' variable returned to the script is an array with one element for each entry. You need to loop through the elements of this array to process them (maybe adding them up, or whatever else you need to do with them)
1

Try this it may help you

echo "<td><input type='checkbox' name='er[]' value='$price'></td>";
echo "</tr>";

$recharge=$_POST['er'];

foreach($recharge as $val)
{
     echo $val;
}

Or you can just do this without using foreach

$arra_val=array_sum($recharge);

you just need to put the value in to some variable and echo it.

4 Comments

Thank you! This totally works! Now all i have to do is add those values. How would I add those?
where do you want to add those values?
@user1551672 I have just modified my answer with another solution check if it works.
@user1551672 You just need to insert this value to some variable and echo it.see my updated answer.
0

If you want to pass a number of elements in an array append the name with square brackets, so it would be:

echo "<td><input type='checkbox' name='er[]' value='$price'></td>";

And the result of $_POST['er'] would be an array of all of the checkbox values.

Comments

0

The input box name should reflect that it's an array by adding the [] suffix:

echo '<td><input type="checkbox" name="er[]" value=" . htmlspecialchars($row['price']) . "></td>';

Then, to process the sum after making sure the input validates as proper values:

if ($ers = filter_input(INPUT_POST, 'er', FILTER_VALIDATE_FLOAT, FILTER_FORCE_ARRAY)) {
    $ercharge = array_sum($ers);
}

1 Comment

@user1551672 Sorry, forgot to add a parameter to filter_input. Updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.