How to sum several numbers of various inputs in php

Asked

Viewed 95 times

-2

At this moment I am attending the 12th Year of a course in Applied Computer to the Web and needed help.

My goal is to build a website page that allows the user the following:

  • Enter the number of fields <input type="number"> you want on the calculator.
  • Add all numbers the user has placed in the respective field <input type="number">.
  • Indicate the result.

All I could do was have the number of fields read by the user, and through a for put them on the page.

My doubt lies in the rest, which is to read every value of each <input type="number">, sum and write the result.

The result of what I’ve been able to do so far is this::

Screenshot da pagina
Click on the image to view in its original size

This is my code so far:

<form method="POST" action="calculadora.php">
                <div class="card-body">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Indique a quantidade de campos que pretende na calculadora:</label>
                        <input name="campos" style="width: 10%;" type="number" class="form-control" id="exampleInputEmail1" placeholder="">
                    </div>
                </div>
                <!-- /.card-body -->

                <div class="card-footer">
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>

            <form action="processo_calculadora.php" method="POST">
                <?php
                if (isset($_POST['campos'])) {
                    $numCampos = $_POST['campos']; 
                    $ab = $numCampos;
                } else {
                    $numCampos = 0;
                    $ab = $numCampos;
                }

                echo "O número de campos escolhido é: " . $numCampos . "<br>";



                for ($x = 1; $x <= $numCampos; $x++) {
                    // echo "The number is: $x <br>";
                    echo "<input type='number' id='$x'>The number is: $x <br>";
                }

                
                ?>
                <input type="submit" value="Calcular">
            </form>
            <?php
            /*
           for ($y = 1; $y <= $numCampos; $y++) {
                    $id[$y] = filter_input(INPUT_GET, $x[$y], FILTER_SANITIZE_NUMBER_INT);

                }*/
                for ($x = 1; $x <= $numCampos; $x++) {
                    // echo "The number is: $x <br>";
                    $id = filter_input(INPUT_POST, $x, FILTER_SANITIZE_NUMBER_INT);
                    for ($x = 1; $x <= $numCampos; $x++) {
                    

                    echo $id . "<br>"; }
                }
            ?>

I’ll be extremely grateful to anyone who can help me.

1 answer

-3


Starts by changing the line where the fields are creating leaving it so:

echo "<input type='number' id='$x' name='numero[]'>The number is: $x <br>";

So the sainda of the values(typed numbers) will be an array, simple

print_r($_POST['numero']);
array('1'=>2,'2'=>5);

At this point you can use the function array_sum or use a for to sum the values;

// Com array_sum
$total = array_sum($_POST['numero']);
echo $total ;

// Com for
$num = 0;
for ($i=1; $i < count($_POST['numero']); $i++) { 
    $num += $_POST['numero'][$i];
}
echo $num;
  • Thank you, I already managed to solve the problem. I only had to change $_GET to $_POST because in my form I put POST. Very grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.