-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::
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.
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.
– Luís Briga