Hi all -

I need some help w/something that I'm not sure what the proper technique is.

How to deal with 'dynamic' forms?

I'm working on an event registration form, where the person who registers normally registers their team members, so the number of team members could range from 1 - n (in theory).

I've found some javascript that creates a new input field (adding 1 to the name, eg. name1, name2, etc.). But I'm not sure how PHP would receive this?

I believe it would have something to do with "for each" but again, I'm not really sure.

Can anyone help?

Recommended Answers

All 4 Replies

You can create predictable field names using a subscript in PHP quite easily (e.g. name=aaa$i) if you know how many you need. If you are creating them on the fly, then javascript is obviously the way to go.

You can use a while loop with an index value to see if a variable exists:

$i = 1;

while (isset($_POST[aaa$i])) {
   ... process the variable ...


I didn't really understand your post.

I was confused by the fact that there weren't any simple quotes (') between the post brackets. To give a simplified version of what I want to do.

From a form like:

<form method="post" action="formprocess.php" name="form">
		Name 1<input  type="text"	name="name1" /><br />
		Email 1 <input  type="text"	name="email1" /><br />
		Name 2	<input  type="text"	name="name2" /><br />
		Email 2 <input  type="text"	name="email2" /><br />
		Name 3	<input  type="text"	name="name3" /><br />
		Email 3 <input  type="text"	name="email3" /><br />
		Name 4	<input  type="text"	name="name4" /><br />
		Email 4 <input  type="text"	name="email4" /><br />
		Name 5	<input  type="text"	name="name5" /><br />
		Email 5 <input  type="text"	name="email5" /><br />

Then in the receiving php I need to process each field (I'll be inserting it into a db and then sending each user an email letting them know they've been registered). As I stated before, the user could (through javascript) add more than 5 users, up to n users in reality.

How would I go about processing this?

Looking at what you sent I wasn't really sure how to process the variable.

Thanks again for your help!

Your variables will all be part of the $_POST array in the receiving program. Thus, your job is to loop through the array determining if each successive variable exists and then doing whatever processing you need to do.

In general, PHP accepts $_POST[name1] or $_POST as the same thing. I believe that the official version has the quotes but they both work most of the time.

In this case, we need to create the name of the array element. You could do that by:

$element = "name".$i;
while (isset($_POST['$element'])) {
   // - or - 
while (isset($_POST["name".$i])) {

   // - or - 
while (isset($_POST[name$i])) {

They should all give the same result.

Once you know that the element exists (and you know what the name is) then you can do whatever you want with that variable. If you are storing the info in a database, then that code will be within your While loop.

Hi Chris -

Couldn't get your script to work. From what I could tell, by adding an incrementing number with (.) (I forget the name for this.) the post isn't actually modified... Also, from what I can tell post without simple quote marks (') doesn't work.

I did find a solution however, which was pretty easy:

$name = $_POST['name'];
$email = $_POST['email'];

$numelentos = count($name);

for ($i=0; $i < $numelentos; $i++)
		print ("Name $name[$i]  - Email $email[$i] <br > ");

Using this also requires that the form is modified from creating an incrementing name (eg. name1, name2, etc.) for each additional field created via jquery to using 'name[]' or 'email[]'. This makes the data come out as an array in the php.

I'd have to clean the post data of course, and printing it is a simplified version of what I really wan to do.

Thanks Chris for pointing me in the right direction.

Cheers - Drew

A full simplified version of what I was doing follows:


	<form method="post" action="form.php" name="form">
		Name 1  <input  type="text"	name="name[]"  />
		Email 1 <input  type="text"	name="email[]" /><br />

		Name 2  <input  type="text"	name="name[]"  />
		Email 2 <input  type="text"	name="email[]" /><br />

		Name 3	<input  type="text"	name="name[]"  />
		Email 3 <input  type="text"	name="email[]" /><br />



$name = $_POST['name'];
$email = $_POST['email'];

$numelentos = count($name);

for ($i=0; $i < $numelentos; $i++)
		print ("Name $name[$i]  - Email $email[$i] <br > ");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.