Get Form element names in PHP code

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
teniosoft
Forum Newbie
Posts: 15
Joined: Tue Feb 03, 2004 1:01 am
Location: Portland, Or

Get Form element names in PHP code

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
teniosoft
Forum Newbie
Posts: 15
Joined: Tue Feb 03, 2004 1:01 am
Location: Portland, Or

Post 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.
teniosoft
Forum Newbie
Posts: 15
Joined: Tue Feb 03, 2004 1:01 am
Location: Portland, Or

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply