Page 1 of 1

Get Form element names in PHP code

Posted: Tue Feb 03, 2004 1:01 am
by teniosoft
I would like to be able to get the name of form elements that I create so that I can name them the same as the fields in the database. Then make it easy to create a single form for simple update queries. Is it possible to get the name of the form element.

Posted: Tue Feb 03, 2004 2:45 am
by twigletmac
Not entirely sure what you are looking for but on a form processing page you can have a play with:

Code: Select all

$form_elements = array_keys($_POST);

echo 'The form elements are '.implode(', ', $form_elements);
Mac

Posted: Tue Feb 03, 2004 3:26 am
by teniosoft
That is exactly what I was looking for

The reason that I want to do this and I would like anyones comments on if it is a good or bad idea is this.

Most of the things that I do are related to adding and editing information in a database.

So instead of having to write custom pages for each table that I will be adding and editing I was thinking of creating a generic php page or class that would go ahead and

for the form take the name of the form elements and either add them or update them depending on what was specified.

So if you have a table with lst_nm, frst_nm, address, city

and a form with elements with those names you could do something like this

for each form element if the element exists in the database
(maybe specified by form element name or something instead)

dynamically generate a query that will update or add a record

update $formname set formelement1 = formvalue1, formelement2 = formvalue2, ..., formelementn = formvaluen where user_id = a session variable.

execute query.

So do you think that this is going to be a security problem. Do you think that it will be useful in enough situations to make it worth creating (it might even work with checkbox arrays)

It would certainly save me a lot of time with boring data access and update code.

Your thoughts are greatly appreciated.

Posted: Tue Feb 03, 2004 4:44 am
by teniosoft
so now I am running into this problem

is it possible to say $_POST[1]

or something like

while(n< numberofellement($_POST[])
{
$_POST[$n]
}

sorry it is not real code

I get nothing returned when I try to access the array this way.

Posted: Tue Feb 03, 2004 4:56 am
by twigletmac
Try using a foreach loop, for example to trim all elements in $_POST you could do:

Code: Select all

foreach ($_POST as $key => $value) {
   $_POST[$key] = trim($value);
}
Mac