PDA

Click to See Complete Forum and Search --> : [RESOLVED] many $_POST's


Static
Oct 19th, 2009, 12:15 PM
I have a form (of course) - its built into a table because the user can click a button and add more rows (then fill in more data) - the main issue is the user can remove rows (on the fly) and I want to make the able to move rows up and down (all done with JS)

fields are
item_x
itemdesc_x
qty_x
price_x
total_x

the x = a number...
so it could have item_1, item_2, item_3 - but also could have item_1, item_7, item_8 (because they can delete)

right now, with php I am looping 1 to 10000 and checking if item_X is set then insert into the db. works but its not a good way.


$x = 1;
while ($x < 10000) {
if (isset($_POST['item_' . $x])){
I know.. BAD BAD BAD (its temporary to make it work)

How can I loop thought these in the correct order? do they come through the post in top down order? so I might get
item_7, item_3, item_4, item_9 (if thats how the user arranged them)

does this make sense? :) LOL

kows
Oct 19th, 2009, 01:32 PM
use arrays like I suggested you do in the past. it's silly for you to keep track of field indexes when something else can do it for you.

if the following form is posted to a script:
<form action="post">
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="submit" value="Submit" />
</form>
it would produce a $_POST array that might look something like:
Array (
text[0] => ''
text[1] => ''
text[2] => ''
text[3] => ''
)
as far as the HTML goes, you don't need to worry about indexes at all with your javascript stuff. you could then produce a loop like:
<?php

//loop through the text[] field by keys rather than using for()

foreach($_POST['text'] as $key => $value){

//this is -just- the value of $_POST['text'][$key]
echo $value;

//if you have multiple fields for each key, you could then reference them using:
echo $_POST['otherfield'][$key];

}
?>

this should be easy to adapt to what you might be interested in! if you're not sure how, be more specific about what you want and ask questions.

Static
Oct 19th, 2009, 02:00 PM
but i am using other JS code to calculate the fields of the current row... how can I tell JS which 2 fields to add if I cant hit them with the ID?

the table needs to have a few functions with it...
1 onBlur of the qty / price calculate the total + add all totals up to a final total.
and the rows need to be able to be moved up/down.

so if i just use the array for the name... the rest should work?

Static
Oct 19th, 2009, 02:12 PM
yes, that works! solves the ordering problem :) thanks!

now...im just stuck on this http://www.vbforums.com/showthread.php?p=3635170