954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Identifying form field names and values with foreach loop

I have a simple dynamic form with 5 input fields

<input type=text name=name />
<input type=text name=height />
<input type=text name=length />
<input type=text name=width />
<input type=text name=weight />

If the user needs more than one of the same item they can add additional input fields.

<input type=text name=height_+1 />
<input type=text name=length_+1 />
<input type=text name=width_+1 />
<input type=text name=weight_+1 />

where +1 is calculated using PHP.

However, once the form is posted, I would like to add the combined weight of all the items together, and then insert each item into a database. To achieve this I tried to use the following.

$name=$_POST['name'];
foreach ( $_POST as $name => $value)
{ 
	$field_name=$name;
	$field_value=$value;
}

This is OK when I echo $field_name and $field_value, because I can see a list of all values and from which field they originate. But I would like to assign each value a variable (preferably the same as the input name in the form) so I can perform my calculation and commit each set of data to the database.

I was thinking of something like

$name=$_POST['name'];
foreach ( $_POST as $name => $value)
{ 
	$field_name=$name;
	$field_value=$value;
        if(strstr($fields_name,"height"))
	{
            $height_[]=$value;
        }
        if(strstr($fields_name,"length"))
	{
            $height_[]=$value;
        }
}

This seems very long winded, and prone to error, and still I wouldn't know how many weight fields to add together.

Can anyone help???

Thorby68
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Can you clarify - are you trying to calculate the total weight of multiples of the same item or the total weight of multiple items?

simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

Many thanks for your help.

in one instance I may have

<input type=text name=name />
<input type=text name=height />
<input type=text name=length />
<input type=text name=width />
<input type=text name=weight />

Where I have to calculate the total weight of 1 item, then submit the data to the database with

mysql_query(INSERT INTO table (name, height, length, width, weight) VALUES ('$name', '$height', '$length', '$width', '$weight')

but I will also be faced with the following

<input type=text name=name />
<input type=text name=height_1 />
<input type=text name=length_1 />
<input type=text name=width_1 />
<input type=text name=weight_1 />

<input type=text name=height_2 />
<input type=text name=length_2 />
<input type=text name=width_2 />
<input type=text name=weight_2 />

<input type=text name=height_3 />
<input type=text name=length_3 />
<input type=text name=width_3 />
<input type=text name=weight_3 />

where I need to calculate

$total_weight=$weight+$weight_1+$weight_2+$weight_3;

and add 3 items to the database, where name is the same for all 3 items.

mysql_query(INSERT INTO table (name, height, length, width, weight) VALUES ('$name', '$height', '$length', '$width', '$weight'), ('$name', '$height_1', '$length_1', '$width_1', '$weight_1'), ('$name', '$height_2', '$length_2', '$width_2', '$weight_2'), ('$name', '$height_3', '$length_3', '$width_3', '$weight_3')

Indeed, the instances of height, length, width, weight could be from 1 to 100, dependent on the user using the form.

Again many thanks, I hope this helps.

Thorby68
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This seems a bit wasteful. How about using name arrays in your forms?

SO initial form fields:

<input type='text' name='name' />
<input type='text' name='height[]' />
<input type='text' name='length[]' />
<input type='text' name='width[]' />
<input type='text' name='weight[]' />


then you just have multiples of these:

<input type='text' name='height[]' />
<input type='text' name='length[]' />
<input type='text' name='width[]' />
<input type='text' name='weight[]' />


Are you adding them interactively via javascript? If not, perhaps an idea to do so, e.g. with an [+] button or similar.

Your server-side form handling code then, just needs to do this:

$no_items = count($_POST['height']); //returns number of items sent
$weights = (array) $_POST['weight']; 
$total_weight = array_sum($weights); //maybe should be checked for is number - you could use array_map for this


Again the table setup looks a little odd as you have repeat fields, a better setup POSSIBLY would be:

ITEMS
item_id (PK)
item_name

ITEM_DETAILS
itemd_id (PK)
item_id (FK)
weight
height
length
width

Just a thought. As I have no idea how this data should be used, I can't say.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Many thanks I'll give it a go...It may also help me sort out the data in preparation for the database insert.

Also, I'll be sure to post my findings so it may help others.

Thorby68
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Many thanks ardav.
With your considerable help I've been able to prepare my data and insert said data into DB.

Form
I have one constant

<input type='text' name='customer number' value='1'/>


I also have a set of data items that can be repeated unlimited times

<input type='text' name='weight[]' />
<input type='text' name='length[]' />
<input type='text' name='width[]' />
<input type='text' name='height[]' />

If I need to add more items, I use javascript newdiv.innerHTML and ni.appendChild(newdiv) to repeat above.

Server Side

// get the constant
$customer_number=$_POST['customer_number'];

//get data arrays by type
$weight = (array) $_POST['weight']; 
$length = (array) $_POST['length'];
$width = (array) $_POST['width'];
$height = (array) $_POST['height'];

//Count number of times each set of data occurs (-1 accounts for first item being zero)
$no_items = count($_POST['weight'])-1;

//start counter to help process data
$num=0;

//loop through each array to ensure each item is correctly matched with it's data set
while($num<=$no_items)
{
	$weight[$num]=$weight[$num];
	$length[$num]=$length[$num];
	$width[$num]=$width[$num];
	$height[$num]=$height[$num];
        
        //add set of data and constant to database
        $query=mysql_query("INSET in table (customer_number, weight, length, width, height) VALUES ('$customer_number', '$weight[$num]', '$length[$num]', '$width[$num]', '$height[$num]')";

        //increase number for new pass of the loop
        $num++;
}


Job Done!
Many Thanks

Thorby68
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Glad tohear. Mark thread solved.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You