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???