Get Form element names in PHP code
Moderator: General Moderators
Get Form element names in PHP code
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Not entirely sure what you are looking for but on a form processing page you can have a play with:
Mac
Code: Select all
$form_elements = array_keys($_POST);
echo 'The form elements are '.implode(', ', $form_elements);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.
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try using a foreach loop, for example to trim all elements in $_POST you could do:
Mac
Code: Select all
foreach ($_POST as $key => $value) {
$_POST[$key] = trim($value);
}