Page 1 of 1

How to process a dynamically created form.

Posted: Tue Nov 07, 2006 8:54 am
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.

Posted: Tue Nov 07, 2006 9:14 am
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, ...).

Posted: Wed Nov 08, 2006 1:28 am
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?

Posted: Wed Nov 08, 2006 1:32 am
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">

Posted: Wed Nov 08, 2006 1:44 am
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.