0

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?

6
Contributors
7
Replies
11
Views
7 Years
Discussion Span
Last Post by SolidSolutions
0

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.
Ex

$var=$_post['first_name'];
for($i=0;$i<count($var);$i++)
{
echo $var[$i];
}
0

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.

0

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.

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

Edited by phpbeginners: n/a

0

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);
for($i=0;$i<count($keys);++$i){
  $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:

// UNTESTED CODE
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.

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

[Links]
http://www.google.com/search?q=iterating+through+$_POST

Edited by Nyight: n/a

0

, 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 />';
}
0

Are you familiar with jquery?

I think this is what you are looking for:
http://jquery.malsup.com/form/


Dan

Edited by Ezzaral: Snipped &quot;fake sig&quot; link. Please restrict such links to your site-wide user signature, which can be edited from the user control panel.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.