PHP / HTML input type number
is there a way to iterate through a lot of input type numbers and only print out the values that have a value greater than 0?
Let's say I have 10
<form action="displayInfo.php" method="post">
<table cellpadding="6">
<tr>
<th>Description</th>
<th>Price</th>
<th>Weight</th>
<th>Image</th>
<th>Quantity</th>
</tr>
<!-- PHP LANGUAGE -->
<?PHP
$sql = "SELECT * FROM parts";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorIndo()));
while( $row = $q->fetch(PDO::FETCH_ASSOC))
{
echo '<tr>' .
'<td>' . $row['description'] . '</td>' .
'<td>' . $row['price'] . '</td>' .
'<td>' . $row['weight'] . '</td>' .
'<td> <img src="' . $row[pictureURL] .'" alt="' . $row[number] . '" style="width:50px;height:50px;">' .
'<td> <input type="number" name = "' . $row[number] ." id= "'. $row[number] . '" value="0">' .
'</td>' . '</tr>';
}
echo "</table>";
?>
</table> <br>
<input type="button" id="submit" value="Submit" />
</form>
So they are dynamically created with an ID that has the values 1,2,3, ... 10. The values are then updated via user input.
Is there anyway I can catch the data submitted by the submit button for user input values that are greater than 0? If so, how do I pass a description or even $ row [number] along with the value that is associated with it. input type number id = $ row [number] as shown in the code.
source to share
The easiest way is probably to use the PHP function explained in How do I create arrays in HTML <form>
?
In short, you rename the form controls to use a common prefix, add square brackets to force PHP to convert them to an array, and then encode the data like any other array:
'<td> <input type="number" name = "row[' . $row[number] . ']" id= "'. $row[number] . '" value="0">' .
I also corrected the missing quote, but I am assuming that it is not in your actual code.
Then:
foreach ($_POST['row'] as $number => $value) {
}
And since it is an array, you can use regular goodies like:
$rows = array_filter($_POST['row'], function($val){
return is_numeric($val) && $val>10;
});
source to share