tags:

views:

40

answers:

3

Using PHP; is there a way to check what type of form field was used to enter info in a form.

For example: was it submitted via a list/menu, radio button, text field, textarea, or checkbox.

I have this info stored in the database; but I'm trying to see if there is a way to do it without querying the database or using hidden form fields to pass the field type. Basically is there a function that already does this?

+2  A: 

The data entered into a form will be submitted as a set of key:value pairs only.

With standard HTML form elements only there is no way of telling what type of form field was used to gather a particular value.

Jon Cram
+4  A: 

I don't know of one though I'm sure someone else might pop up with an answer. But if the forms in question are of your own design you could name the inputs as checkbox_ or textarea_ prepended to your normal name. Then parse them on the form processing side.

You can even do something like `checkbox[sex]`, `checkbox[OS]`, `text[first_name]`, `text[last_name]`, then all of your checkboxes will be under `$_POST['checkbox']` and everything under `$_POST['text']` will be text.
Lèse majesté
A: 

Only name/value pairs are passed through the Post data, so you would need to mark it in the field name to give your server script a hint of what it is. You could do this with a prefix/postfix. Depending on where you are in your project, you may want to look into using a framework and taking advantage of the advanced form handling options that they can give you.

tsgrasser