How to process a dynamically created 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
tcollogne
Forum Newbie
Posts: 2
Joined: Tue Nov 07, 2006 8:46 am

How to process a dynamically created form.

Post by tcollogne »

Hi all,

I am new to php and I am trying to do the following :

I want to create a php page with a html form. This html is dynamically generated. This means that I don't know the amount of fields on the form and
I also don't know the name of the fields.

The generation of the form is no problem, but what I don't know is how to process this form.

How can I do this? I know that I can use $_POST and specify the name of the form field to retrieve the value, but in my case I don't know the name,
so I have no idea how to implement this.

I have a good knowledge of jsp combined with struts or jsf, so I am not a total noob. :)

Can somebody help me, please?

Thank you.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

This will iterate through all data in POST

Code: Select all

foreach($_POST as $field_name => $field_value)
{
    //$field_name will be name of the field and $field_value - user input.
    //...
}
or

Code: Select all

while(list($field_name, $field_value) = each($_POST))
{
    //...
}
Don't forget to escape user input whatever you will do with it (DB, Email, show in page, ...).
tcollogne
Forum Newbie
Posts: 2
Joined: Tue Nov 07, 2006 8:46 am

Post by tcollogne »

Thanks. It was something like that that I was looking for.
The only thing is that the form is a question form with multiple choice answers. So for each question, there are five possible answers.
It is also possible to choose more than one answer, so I think the best way to do this, is a "multibox".

How can I retrieve values from a collection of checkboxes? If I use your example, does this mean that $field_value will be an array?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you can make a form element an array by adding square brackets after the name...

Code: Select all

<input type="checkbox" name="checks[]" value="check_one">
<input type="checkbox" name="checks[]" value="check_two">
<input type="checkbox" name="checks[]" value="check_three">
<input type="checkbox" name="checks[]" value="check_four">
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

tcollogne wrote:Thanks. It was something like that that I was looking for.
The only thing is that the form is a question form with multiple choice answers. So for each question, there are five possible answers.
It is also possible to choose more than one answer, so I think the best way to do this, is a "multibox".

How can I retrieve values from a collection of checkboxes? If I use your example, does this mean that $field_value will be an array?
just try it out. should be very simple. build as complex form as you wish....fill in some data..submit.

on php just var_export,var_dump,print_r $_POST and you will see how data comes to you. then process as needed.
Post Reply