Jump to content

[SOLVED] Dynamic fields/variables with PHP/HTML


guyfromfl

Recommended Posts

I am trying to make an order form that starts with 1 row of fields.

 

example:

 

txtPartNum, qty...whatever else you need...

 

so the first row would say

 

txtPartNum1, qty1

 

then if you add another add a row

 

txtPartNum2, qty2...

 

and so on until you have added enough rows for all the parts in the order.

 

my problem is once you've POSTed the data,  the number at the end (qty2) isn't dynamic anymore.

 

i want to make a for loop that loops through all the rows the order needs and pass this data on to insert into a SQL database?

 

something like this:

 


<?php

$count = $_POST['count']     // the number of rows for this order

// loop through the total number of items
for ($item=0; $item<$count; $item++) {
   $sql = "INSERT INTO orders (customer, partNum, qty) VALUES ($_POST['custNum<need a variable here>', $_POST['txtPartNum<var>', $_POST['qty<var>'])";
?>

 

btw: thats a basic idea i know there was no validation, formatting and syntax perfection...  :P

 

Im trying to get around limiting the orders to say 10 items, or creating a new order for each item.

 

I hope that made sense to somebody.

 

Thanks

mark

 

 

You can post into array form

 

one input uses var[] for its name

another uses var[] for its name

yet another uses var[] for its name

 

the first input is accessible by var[0], the second var[1], etc.

 

combine that with a simple foreach loop such as

 

foreach($_POST['var'] as $input) {}

 

or, alternatively

 

for($i = 0; $i < count($_POST['var']); ++$i) { 
   echo $_POST['var'][$i]; // or whatever
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.