I have a form with 3 text fields and a button that you use to extend the form and give you another 3 text fields below it for the same information and so on with the button... my problem is how do I total these fields? When i fill them in and hit submit (say I have 2 rows worth) it only displays the final rows values.
echo "<tr><TD> <input type = 'text' name = 'name'/></td> <td> <input type = 'text' name = 'type'/> <td> <input type = 'text' name = 'cost'/> </td></tr>";
I know its because it keeps using the same names over and over each time they add a row and overwriting the old data.. but how can I make it like into an array because its html and instead use php.. how can you edit those variables so that its like name = 'name[0]' name='name[1]' and so on instead of name = 'name'. Or is there another way to do this?
on the target php file it just takes the variables and displays them or its supposed to add them up but I only get the last field row of values
$name = $_POST['name'];
echo "<h2>$name</h2>";
eg.
form text fields
row1 name = bob type = blah cost = 10
row2 name = smith type = f cost = 3
so I'm only getting the row 2 values or if I added row 3 i'd only get row 3 values
Passing multiple data from a form to a php page
Moderator: General Moderators
- Templeton Peck
- Forum Commoner
- Posts: 45
- Joined: Sun May 11, 2003 7:51 pm
it's working exactly the way you describe it but it's still html what the browser processes.
try
try
Code: Select all
<html>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
if(isset($_POST['data'])) // maybe check wether it's really an array
{
for($i=0; isset($_POST['data'][$i]); $i++)
{
?>
<input type="text" name="data[<?php echo $i; ?>]" value="<?php echo htmlentities($_POST['data'][$i]); ?>" /><br />
<?php
}
}
else
$i=0;
?>
<input type="text" name="data[<?php echo $i; ?>]" value="" /><br />
<input type="submit" />
</form>
</body>
</html>- Templeton Peck
- Forum Commoner
- Posts: 45
- Joined: Sun May 11, 2003 7:51 pm