Reputation: 3751
Working on making an inventory page for work. I have written a page that will loop through my data base and display all of the items in there.
include 'auth.php'; //to change login, please authenticate
$sql="SELECT * FROM `inventory` ORDER BY `id` asc;";
$result=mysql_query($sql);
while($rows = mysql_fetch_array($result)){
echo $rows["name"];
<input type="text" name="<? echo $rows["id"]; ?>" id="<? echo $rows["id"] ?>" placeholder="Who will go in here?" />
}
The above code is doing what I want. I would like to put this in a form and have a submit button. Let's say the form is
<form method="POST" action="page.php">
Now I want to be able to write a page.php so that it can handle all of the data regardless of the number of items. In the past I have done the following
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone=$_POST['phonenum'];
$email = $_POST['email'];
$age = $_POST['b18'];
But that won't work as I will have an unknown amount of post. Please write any code you like. I prefer document pages.
Upvotes: 2
Views: 2844
Reputation: 33439
$_POST is just an ordinary hash array, so you can loop over it
foreach ($_POST as $key => $value) {
print "{$key}: {$value}<br />";
}
edit
well you need to do some special adjustments, if your values are array values (POST key with [] at the end of the name)
edit it's of no use if you try
foreach ($_POST as $varname => $value) {
${$varname} = $value;
}
because you don't know the name of the variables;
Upvotes: 6
Reputation: 2763
I will start from your code
include 'auth.php'; //to change login, please authenticate
$sql="SELECT * FROM `inventory` ORDER BY `id` asc;";
$result=mysql_query($sql);
while($rows = mysql_fetch_array($result)){
echo $rows["name"];
<input type="text" name="id[]" id="<? echo $rows["id"] ?>" value = "<? echo $rows["id"] ?>" placeholder="Who will go in here?" />
}
Now in your page.php
<?php
foreach($_POST['id'] as $id):
echo $id;
endforeach;
?>
This will print out all the values of the input named id[] What i did for you is naming the input with array at the end and then print that array I hope that's what you want
Upvotes: 0