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

 

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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