Hi,

I was wondering if it is possible to fetch all items in a form in one go in PHP rather than using $_POST[] or $_GET[] for each individual element in the form.

I often deal with forms having more than 30-35 elements and it get really tedious (not to mention boring ;) ) to do it individually. I have tried looking around but in vain.

Thanks!

Recommended Answers

All 8 Replies

Member Avatar for diafol

You can give all your inputs the same name:

<input id="widget1" name="myname[34]" />
<input id="widget2" name="myname[17]" />

You can make the name just myname[] if you want a key will be assigned automatically.

This will place all the form widget values into a POST array array:

$_POST['myname']

This doesn't really make things easier than looping the parent ($_POST).

It's useful for separating your validating procedure:

$_POST could be for errm, textboxes.

Anyway, valdiating/sanitizing is tedious. You need to ensure that every element is locked down tighter than a witchfinder general's thumbscrew.

I have tried looping but somehow, in many cases, they tend to make things even more tedious. I think its time PHP makers think about it.

By the way, nice one:

Anyway, valdiating/sanitizing is tedious. You need to ensure that every element is locked down tighter than a witchfinder general's thumbscrew.

Do this:

foreach($_POST as $var=>$val) $$var=$val;

Now you have all your form elements in variables. Obviously you ca put them in an array instead or process them in different ways depending on the $var name.

Thanks! Thats interesting. Let me try this one.

I am not sure if this helps as well , but there is this one as well

extract($_POST);

this will create variables having the names excatly the same as defined in the form and it will only make variables for elements which have an input value assigned to it.

example

<input type="text" name="tex1" />
<input type="text" name="tex2" />

using the extract statement will create the variables $tex1 and $tex2 with values corresponding to the ones in the form.

if for example there was no entry made for tex2 , then it will not create a variable named $text2

If you use extract() note the warnings in the documentation -- security concerns exist when using it with form data as hackers can add extra data which could overwrite some of your script's variables. You should avoid using the default EXTR_OVERWRITE extract_type parameter for such use.

If you label all your form data consistently, for example all input names start with "x_" (e.g. "x_name", "x_phone", ...), then you can use:

foreach($_POST as $var=>$val) if (substr($var,0,2)=="x_") $$var=$val;

This prevents variable insertion as it only accepts variables of the specified name-type.

Alternatively,

foreach($_POST as $var=>$val) if (substr($var,0,2)=="x_") $postdata[substr($var,2)]=$val;

is another approach, putting data into an array.

Hi...
Before I also have this problem.
It is better to use ADODB.
ADODB is a PHP abstraction class (install adodb to the project folder).
Give name of the form elements as the name of the fields in the database.

At the time of insertion first the simply calls an empty record from database using adodb function.

Then specify the array that contains form data(ie $_POST/$_GET/$_REQUEST) on to an insert function of adodb.

Try it..It will be simple and fast..

If you want more details I wil send example script.

Cheers

Sample Values

<input id="widget1" name="myname[34]" />
<input id="widget2" name="myname[17]" />

Form

<form method="POST" action="">
<input id="widget1" name="myname[]" />
<input id="widget2" name="myname[]" />
</form>

PHP Script Using Array Method

<?php
$arrayValue = $_POST;
$totalArray = count($arrayValue);
if($totalArray<=0)
{
echo "0 selected";
}
else
{
for($x=0; $x<$totalArray; $x++)
{
$value = $arrayValue[$x];
$sql = "INSERT INTO tablename(id,category) VALUES('', $value)";
mysql_query($sql);
header("Location: moveanotherpage.php"); //print successfully inserted then move the page to another location.
}
}
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.