Hey guys,

I was making a form that is um... quite large and all inputs consist of the form looking like

<input type="text" id="first_name" name="first_name" />

so instead of having to do

$first_name = $_POST['first_name'];

and so on for every input, is there a way to grab every 'name' or 'id' from each input within the <form></form> and apply to a variable of the same value of the 'name' or 'id'.

I was thinking of something like a foreach statement??

Any ideas?

Do you mean to say you are using large number of inputs with same name?
If so use <input type="text" id="first_name[]" name="first_name[]" />
Then you can retrieve all values in first_name[] by counting it as an array.

echo $var[$i];

No I have like 50 form inputs each with a different 'name' and 'id' value. and was just looking for semi automated way to assign a each of those input values into a variable then add them to my database.

If the variable names have a consistent format like item1, item2, item3 and so forth, you can process them as variable variables in the program that receives the form output. If all the variable names are different, then you could use a foreach statement to iterate through the POST variables.

use extract($_POST); after you submit your form in action.
echo every[fieldname] and you will see the value;
eg. echo "$first_name";

I've tried doing this several times myself and I've only come up with one reasonably efficient method of doing this:

$keys = array_keys($_POST);
  $value = $_POST[$keys[$i]];

$_POST is not an indexed array so it's impossible to iterate generically through it.

Another potential method that might work:

foreach($key in array_keys($_POST)){
   $value = $_POST[$key];

Given they are pretty much the same thing though I rarely ever use foreach() so I'm not sure if this will work or not.

An html tag's ID, to my knowledge, cannot be sent when the form is submitted.


, I looking into the foreach method this is what i came up with

function filter($data) {
    $data = trim(htmlentities(strip_tags($data)));

    if (get_magic_quotes_gpc())
        $data = stripslashes($data);

    $data = mysql_real_escape_string($data);

    return $data;

foreach($_POST as $key => $value) {
    $data[$key] = filter($value);
    echo $value . '<br />';

Are you familiar with jquery?

I think this is what you are looking for: