Hi everyone, new guy here. I am relatively new to php but I'm trying to create an order form that puts user data in a mysql database.
Now, here's the functionality that I am having a hard time figuring out:
I want them to go to the order form page (let's call it orderpage.php)
On the order page, to make this example simple, lets say there are 3 fields- 'first_name' , 'last_name' , and a drop down menu called 'product_of_choice'
So for example the customer puts "John" ... "Smith" ... "Large pepperoni pie"
But then he also has a friend named Steve Urkel who wants a medium sausage pie, and he wants to add this info to his order.
Now, I don't want anything submitted into the database until they have every single one of their order forms filled out at the same time, so I want John Smith to be able to click a button called "Add another person" at the bottom of the page.
Also I want them to be able to add as many people as they would like.
How do I make this possible, and keep in mind that I want each person's info to go on separate rows in the database. So John Smith and Steve Urkel need to go on separate rows. They should receive the same $order_number but a separate $Unique_ID_number
I have done a fairly extensive amount of research, I mean a lot, and I just cant get a grasp on exactly how to write this code. I have come to the understanding that I need to use sessions, multidimensional arrays, foreach loops and whatever else, but I just can not for the life of me, figure out how to put these things into effect.
Also I'm not asking anyone to write my website for free so please don't think that. If you feel that you would be doing my work for me by answering this question, then tell me how much I need to pay in order to have this answered in a way that I can understand. Just please dont respond by saying "you need to use sessions and arrays" because I know that already. Just can't put this puzzle together properly.
Thanks in advance for any help I may receive and I apologize for the noobishness of these questions.
-Billy
multidimensional array/order form help
Moderator: General Moderators
-
billythekid
- Forum Newbie
- Posts: 8
- Joined: Tue Dec 09, 2014 11:12 pm
multidimensional array/order form help
Last edited by billythekid on Wed Dec 10, 2014 4:44 pm, edited 1 time in total.
Re: multidimensional array/order form help
There are two problems to tackle here:
1. A form with multiple sets of fields
2. Inserting that form data into your database
#1 can be handled a few ways. The simplest is asking the user how many people are ordering (on a previous page) then generating the form accordingly. Works fine up until you're partway through the order process and you decide you want to add or remove somebody. This may be okay for you, I don't know.
A little more complicated is giving the user Add or Remove buttons that submit the form, don't send it for processing, and simply redisplay the form with an additional/one less order. It's certainly better but not very "web 2.0", however it does keep you away from Javascript in case you're not ready for that yet.
The nicest but most complex way is to use Javascript to build the form. It starts with whatever you consider to be a normal number of orders: just one, probably. The user can then click Add or Remove buttons to add or remove orders immediately without having to submit the form. That works by using Javascript to modify the HTML (so to speak) of the form, then when the form is submitted it will have everything the user wants.
If you're learning, I would do all three approaches in order: the first so you get this all working quickly, the second to be fancier, and finally the third to (start to) see how Javascript can do fancy things. It's more work, of course, but I think it's worth the time.
It does leave the question of what the form's HTML should look like. That's best answered by constructing the form how you see fit, then you can post it here and we can show you what changes to make to it.
#2 depends a lot on the form's HTML (which dictates how it appears to PHP) but the basic idea is you have a loop that inserts one record after another, as it gets each order from the form. If you can insert one row then you're 95% of the way to inserting multiple rows.
1. A form with multiple sets of fields
2. Inserting that form data into your database
#1 can be handled a few ways. The simplest is asking the user how many people are ordering (on a previous page) then generating the form accordingly. Works fine up until you're partway through the order process and you decide you want to add or remove somebody. This may be okay for you, I don't know.
A little more complicated is giving the user Add or Remove buttons that submit the form, don't send it for processing, and simply redisplay the form with an additional/one less order. It's certainly better but not very "web 2.0", however it does keep you away from Javascript in case you're not ready for that yet.
The nicest but most complex way is to use Javascript to build the form. It starts with whatever you consider to be a normal number of orders: just one, probably. The user can then click Add or Remove buttons to add or remove orders immediately without having to submit the form. That works by using Javascript to modify the HTML (so to speak) of the form, then when the form is submitted it will have everything the user wants.
If you're learning, I would do all three approaches in order: the first so you get this all working quickly, the second to be fancier, and finally the third to (start to) see how Javascript can do fancy things. It's more work, of course, but I think it's worth the time.
It does leave the question of what the form's HTML should look like. That's best answered by constructing the form how you see fit, then you can post it here and we can show you what changes to make to it.
#2 depends a lot on the form's HTML (which dictates how it appears to PHP) but the basic idea is you have a loop that inserts one record after another, as it gets each order from the form. If you can insert one row then you're 95% of the way to inserting multiple rows.