Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing multiple form fields 1

Status
Not open for further replies.

flugh

Technical User
Aug 23, 2002
655
US
I am setting up a form that will have 20 'rows', each one holding several <input> fields (probably 3, invoice number, customer name, and zip code). These will get inserted into a mysql database later. When I submit the form, I need to loop through the data, build my INSERT query for each row, and send it to the mysql server. My problem lies in the looping through data. I just can't seem to Google up an Idiot's Guide to doing this :)

I figure you generate the form with php using a 'for x = 1 to 20' type loop. The name of each row is, say
Code:
&quot;manifest&quot; . $x
. How do I get each row and manipulate it on the backend? Probably a 'variable variable' ($incomingrow$$x)?

Links to good tutorials are appreciated. Hand-holding accepted too. Sorry, this has to have been asked a million times, I just can't seem to find the right stuff to search for [neutral].



----
JBR
 
This is quite simple, actually. Instead of naming the rows with numbers, name each field in the row with the row number. So you would have invoice_1, customer_1, zip_1. Also set up a hidden field at the end of the form that stores the max number of rows.

Then, on the receiving PHP page build a for-loop off of the hidden field like this:

Code:
for ($i=1; $i<=$_POST['hdnRowNum']; $i++)
{
    $strSQL = &quot;INSERT INTO table (invoice, customer, zip)
VALUES ('&quot; . $_POST['invoice_' . $i] . &quot;', '&quot; . $_POST['customer_' . $i] . &quot;', '&quot; . $_POST['zip_' . $i] . &quot;')&quot;;
    $objRS = mysql_query($strSQL);
}

I think that should pretty much do it. Let me know if it doesn't work for you or if you have any other questions :).

Take Care,
Mike
 
Thanks, that looks like it's the trick for that page :)

What do you think about a more 'dynamic' situation? I just got done using some drag-n-drop stuff (found at and have a vision of dragging and dropping images (php created) of trucks on locations they will be used at each day. In order to get it into the database, I'm figuring a form with all the fields hidden, use javascript to get coordinates when dropping the images, parse it on the backend with php. I'm thinking each 'row' in that form could use the unique truck number. This could be 5 trucks in a given day, or up to maybe 15.

I gotta slow down. The Stacker 2 and 2nd liter of of Coca Cola are getting to me. Thanks for the heads up. If you have any thoughts about the other stuff I mentioned, I'm all ears :)

----
JBR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top