Aamir Mahmood
Aamir Mahmood

Reputation: 2724

Post HTML elements array to PHP

Here is the snapshoot of my form. The input values under LPO (1st column) are hidden, I just showed them here to show complete form.

enter image description here

if color_id (first left most inputbox) is 37 its DB value is BB552 as displayed. And if its 3, the value of that color is BB110, but when its 0, it means a user has selected Custom and written a value by him self in the third row it is FFBBBAA.

I need to submit the form to PHP

The field names are as follows

color_id[] <-- Hidden input
order_id[] <-- Hidden input
subcolor_id[] <-- Select
subcolor_string[] <-- Input
material_id[] <-- Select
weight[] <-- input

When I post the form, in PHP

<?PHP

$count = count($_POST['weight']);

for($i = 0; $i < $count; $i++){
    $color_id = $_POST['color_id'][$i];
    $order_id = $_POST['order_id'][$i];
    $material_id = $_POST['material_id'][$i];
    $weight = $_POST['weight'][$i];

    // i can count one post item and can iterate over all.

    if($color_id == 0){
       $color_id = $_POST['subcolor_id'][$i]; // this give me wrong result;
    }
    
}

So when 0 is there, admin approving this form, can leave it to custom or change the color back to some value from DB

I want to know what are the possibilities for getting the proper sub color values if first hidden input color_id has 0.

So far the one which I thought of is to add two more hidden fields in Sub Color columns to match their index, but this will require me to completely re-write the del sub color code

Is there any other way which can save me from doing lots of alterations to this form?

Upvotes: 1

Views: 283

Answers (2)

Sriniwas
Sriniwas

Reputation: 515

as eds mentioned it totally makes sense.. u shud do this way

$j=0;
$count = count($_POST['weight']);

for($i = 0; $i < $count; $i++){
$color_id = $_POST['color_id'][$i];
$order_id = $_POST['order_id'][$i];
$material_id = $_POST['material_id'][$i];
$weight = $_POST['weight'][$i];

// i can count one post item and can iterate over all.

if($color_id == 0){
   $color_id = $_POST['subcolor_id'][$j]; // this give me wrong result;
   $j++;
}

}

this shud solve your problem..

Upvotes: 3

eds
eds

Reputation: 439

Your other option is to declare another iterator $j outside of the loop, starting at 0, and manually incrementing that iterator after you read a subcolor each time. Then it should always be the index of the next subcolor_id you need to read.

Upvotes: 2

Related Questions