Reputation: 880
I'm trying to build an online ordering form for a restaurant. There are different types of food items that require different input fields. I'm using jQuery to add these input fields after the user clicks a link to add a certain product. I'm basically appending these form fields to a div so the user can fill them out.
After the user is done adding items and click the submit button I want to use PHP to send the order to an email address. I know how to send email in PHP, but I'm not sure how to get all of the form data into the email.
I won't know how many fields the user adds until they hit the submit button. My question is, once the user hits submit what can I do to get all of the form fields and spit it out into a body string for the email? Can I use an array to store all of the form data and go through the array in PHP and get all of the data out of it?
Thanks for your time!
Upvotes: 1
Views: 258
Reputation: 3273
All of the form data that is submitted will be in the $_POST
array ... Loop through that and get what you need ...
For Instance (Assuming you know the names of the fields you are getting data from):
foreach($_POST as $field => $value){
//$field will contain the name of the field
//$value will have the value the user entered
//You can add a switch statement to do something specific with the fields
switch($field){
case 'burger':
//do burger stuff
break;
case 'salad':
//do salad stuff
break;
default:
//I don't know what food this is ...
}
}
Upvotes: 1
Reputation: 3212
If you give all of the inputs the same name and use the regular submit, the value for that name will be a comma-seperated list of the things added. If that's what you want, it's easy to do.
Upvotes: 0
Reputation: 28356
You can simply examine the $_REQUEST['NAME_OF_USER_INPUT']
or $_POST['NAME_OF_USER_INPUT']
for submitted data, and according to this field construct your email text.
Upvotes: 0