NaDie
NaDie

Reputation: 112

Array of a single field

I'm quite confused on how to access the values of a field, i have 2 fields which accepts several inputs let's call it r_id[ ] and qty[ ] and i need to access each of their values independently

now supposedly this should loop depending on how many r_id's are inside the input post

foreach ($_POST['r_id'] as $row)

r_id is used to verify how many qty s should i put on a r_id

input e.g:

r_id=1,2
qty=3,16

what should happen is on a row with an r_id of 1 i will take the old qty with a sample value of 5 and minus the new qty 3 and then on the second iteration r_id 2 old value of qty minus new qty 16 and so on

these are the forms i used

<input type="text" name="r_id[]" class="form-control inline" value="" required>
<input type="number" step="0.001" name="qty[]" class="form-control inline" value="" required>

i made a javascript to add similar fields up to 10 so that explains why i use an array

Upvotes: 2

Views: 63

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

To process those 2 HTML array fields from the $_POST array you need to do something like this :

Remember both $_POST['r_id'] and $_POST['qty'] are arrays and they should be delivered to you in the same order they appear on the HTML page. So you can process over one of them in a foreach and then address the other directly

So as a simple example of looking at what was entered :-

foreach ($_POST['r_id'] as $idx => $id) {
    echo 'r_id - ' . $id . ' was entered with qty ' . $_POST['qty'][$idx];
}

Upvotes: 1

Related Questions