简体   繁体   中英

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.

在此处输入图片说明

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM