Passing multiple data from a form to a php page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Passing multiple data from a form to a php page

Post by Templeton Peck »

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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it's working exactly the way you describe it but it's still html what the browser processes.
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>
User avatar
Templeton Peck
Forum Commoner
Posts: 45
Joined: Sun May 11, 2003 7:51 pm

Post by Templeton Peck »

thanks thats helped out quite a lot :)
Post Reply