Accessing repetative variables from a form

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
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Accessing repetative variables from a form

Post by kendall »

Hi,

I have a form in which i collect consecutive email and passwords
i.e. email1 pass1 email2 pass2
which i would then insert into a database

so far i have

$x=1;
while($x<=count($_POST)){
if(array_key_exists("Email".$x,$_POST)){
$Email = 'Email'.$x;
$Pass = array_push($_POST,$_POST['Password'.$x]);
$account[$Email]=array_push($Pass);
$x++;
}

}
while(list($k,$val)=each($account)){
echo "Email: ".$k." Password: ".$val."<br>";
// Note using this to test how the calues would come out
}
exit;
}

but im getting a timeout

im trying to figure out a simpler way but dont quite have the nail on the head

any suggestions?

Was there a way in the form i could have an array of the values already defined?
thus email1 email2 pass1 pass2 would be Email[] array Pass[] array
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Accessing repetative variables from a form

Post by kendall »

opps

that line should be

$account[$Email]=$Pass;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try this one

Code: Select all

<html><body>
<p><table border="1">
	<tr><th>email</th><th>password</th></tr>
<?php
if (isset($_POST['ep']))
{
	foreach($_POST['ep'] as $entry)
		echo '<tr><td>', $entry['email'], '</td><td>', $entry['pass'], '</td></tr>';
}
?></table></p>


<form method="POST">
	email:<input type="text" name="ep[0][email]" /> password:<input type="text" name="ep[0][pass]" />
	<br/>
	email:<input type="text" name="ep[1][email]" /> password:<input type="text" name="ep[1][pass]" />
	<br/>
	email:<input type="text" name="ep[2][email]" /> password:<input type="text" name="ep[2][pass]" />
	<br/>
	<input type="submit"/>
</form>
</body></html>
Post Reply