I have an order form which had 5 part that repeated themselves to allow a person to order up to 5 different items. I originally did it by having $item1, $quantity1, $type1, $item2, $quantity2...etc hardcoded into both the PHP processor and the HTML form (e.g. values of form elements were item1, quantity1, etc).

I have now re-coded it so it uses arrays for both the HTML form (the parts that repeat are in the array) and the PHP processor, and all is working fine. Both currently use a variable $itemamount = 5 which is hardcoded into the script.

The only thing is, now i have my arrays i was wondering if i can make it so users can somehow input/specify how many items they wish to add? Ideally i don't want to use POST as the form using the arrays has an "if POST" thing that it could mess up. The other slightly problem is that the page is included in a larger CMS type system which means using GET is impossible as you can't get the value into the URL (e.g. by using form.php?itemamount=3)...so i wondered if theres another way of doing this?

Basically i either want a seperate page that asks the user how many items he/she wants to order, which then forwards them onto form.php that will give them the form to fill in. Alternatively, if all the code was on the form page and they (by default) have 1 item field and could then somehow click "Add Item..." if they wished to add more and more as many times as they want?

So, any ideas? I'm a little stuck so appreciate any help!

Inigoesdr

03-09-2008, 08:39 AM

You have several ideas there, any of which would work. The easiest of your suggested options is probably having a separate form with a drop-down with the number of items, and a submit button that said "Update Number of Products" or something similar. You could also use Javascript to dynamically update the page without reloading.

echeats

03-09-2008, 03:09 PM

I'd really like to do the Javascript option (due to the possible problems mentioned with GET and POST above) but am not sure where to start, but i really like the idea of not having to reload the page.

Do you have some code that would allow me to do that? Im not sure where to start.

CFMaBiSmAd

03-09-2008, 03:44 PM

I recommend starting simple, then add features that match your level of programming skills. If you have experience with javascript, then dynamically building the form with the requested number of fields or simply having an "add item" click-able link that adds a row of inputs each time it is clicked is certainly possible.

For your existing code that uses arrays, it is not necessary to carry around a variable that says how many elements there are. Just use a foreach() loop. This will also allow the form processing code to work for any number of input fields without needing to set or change anything to match the number of elements.

For your dynamic number of items, the simplest method, and this will work even if someone has turned off javascript in their browser, would be to have a drop down on your form to pick the quantity. Then have a small submit button (or use the onchange event) to submit this number to your form processing code. Your form processing code would then build and output a form with the requested number of fields.

For the next step up, there are tons of "add form field" javascripts that you can find through searching on the Internet.

echeats

03-09-2008, 06:21 PM

For your dynamic number of items, the simplest method, and this will work even if someone has turned off javascript in their browser, would be to have a drop down on your form to pick the quantity. Then have a small submit button (or use the onchange event) to submit this number to your form processing code. Your form processing code would then build and output a form with the requested number of fields.

Any example code for this part? Google doesn't like me :(

echeats

03-10-2008, 09:45 AM

I've still not had any luck finding the code that would let me do this. I know what needs to happen but just cant write it from scratch as wouldn't know where to start.

Can anyone point me in the right direction?

abduraooft

03-10-2008, 09:55 AM

Any example code for this part? Google doesn't like me :(
When I used the quick search in this forum for the key words dynamic form, I got plenty of results.
http://www.google.com/search?q=site&#37;3Acodingforums.com+dynamic+form&qfront=dynamic+form

echeats

03-10-2008, 12:15 PM

I tried using your code here (http://codingforums.com/showthread.php?t=132080) but didn't have any luck as what that is doing is alot simpler than the fields i am trying to add. I can't find anything that allows me to put a lot of code into the hidden div, most of them are like your's and use JS to add just a few text boxes?

Here's the code i'd like people to be able to add/remove (like in your code)...