May 18, 201214 yr Hi, i have my page setup so that people who visit the site order form are firstly prompted to submit how many guests they are ordering for. Once they have chosen there number, e.g. 4. They submit this and the page reloads and gives them a table with 4 rows (or whatever number they selected) that allows them to enter the guests details: name, starter, main, dessert. This information is then submitted and sent in an email to the restaurants email address. The problem i have is that the restaurant asked me to allow the form to submit upto 40 people if they wanted. So when the email is sent to the restaurant, if it was only for 4 people... it will give them details, but then the rest of the email will have 36 blank entries like so: Person 1: John Doe Person 2: Fred Doe Person 3: Julie Doe Person 4: Frank Doe Person 5: Person 6: Person 7: Person 8: Person 9: Person 10: ....etc up to 40. The solution i need is so that i can make it only send the email with the number of people WITH data, and to not send it with the people WITHOUT data. my code is below. for lack of repeating, i have only used upto 5 people, not 40. The first thing they see is this simple select option form ONLY: <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table class="guests"> <tr> <td><p>How many people are you booking for?</p></td> <td> <select name="guests" id="guests"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select></td> <td> <input type="submit" value="Submit" id="submitguests" name="submitguests" /> </td> </tr> </table> </form> Once that is submitted they then see this form: <?php if(isset($_POST["guests"])) { echo " <form method=\"post\" action=\"orderform.php\"> <div class=\"fodiv\"> <table class=\"booker\"> <tr> <td><p>Name of Person Booking</p></td> <td><input name=\"fullname\" type=\"text\" id=\"fullname\" /></td> </tr> <tr> <td><p>Date of Booking</p></td> <td><input class=\"date-pick\" type=\"text\" name=\"bookdate\" id=\"bookdate\" /></td> </tr> <tr> <td><p>Phone Number</p></td> <td><input name=\"phonenum\" type=\"text\" id=\"phonenum\" /></td> </tr> <tr> <td><p>Email</p></td> <td><input name=\"emailaddr\" type=\"text\" id=\"emailaddr\" /></td> </tr> </table> <br /><br /> <p>You are booking for <span class=\"big\">{$guests}</span> people. Please enter your order below. Thank You.</p> </div><!-- fodiv --> <table class=\"people\"> <tbody> <tr> <th class=\"col1\"><p> </p></th> <th class=\"col2\"><p>Name</p></th> <th class=\"col3\"><p>Starter<br /><span class=\"small\">(Choice of Dips if applicable)</span></p></th> <th class=\"col4\"><p>Main Course<br /><span class=\"small\">Please also indicate (F) for Fries & (r) for Rice if applicable</span></p></th> <th class=\"col5\"><p>Any Dietary Requirements</p></th> </tr> "; } for ($i = 1; $i <= $guests; $i++) { echo " <tr> <th>Person {$i}</th> <td><input name=\"person{$i}\" id=\"person{$i}\" type=\"text\" /></td> <td><textarea name=\"starter{$i}\" id=\"starter{$i}\" type=\"text\"></textarea></td> <td><textarea name=\"maincourse{$i}\" id=\"maincourse{$i}\" type=\"text\"></textarea></td> <td><textarea name=\"dietary{$i}\" id=\"dietary{$i}\" type=\"text\"></textarea></td> </tr> "; } ?> <tr> <th> </th> <td> </td> <td> </td> <td> </td> <td><?php if(isset($guests)){ echo "<input type=\"submit\" value=\"Submit Order\" class=\"submitorder\" id=\"submitorder\" name=\"submitorder\" />";} ?></td> </tr> </tbody> </table> <input name="sendto" type="hidden" value="xxxx@xxxxxxx.co.uk" /> </form> This is the PHP that collects form submit data and sends in email: if(isset($_POST["submitorder"])) { $sendto = $_POST['sendto']; if(isset($_POST['person1'])) { $person1 = htmlentities($_POST['person1']); $starter1 = htmlentities($_POST['starter1']); $maincourse1 = htmlentities($_POST['maincourse1']); $dietary1 = htmlentities($_POST['dietary1']); $guestplaces = 1; } if(isset($_POST['person2'])) { $person2 = htmlentities($_POST['person2']); $starter2 = htmlentities($_POST['starter2']); $maincourse2 = htmlentities($_POST['maincourse2']); $dietary2 = htmlentities($_POST['dietary2']); $guestplaces = 2; } if(isset($_POST['person3'])) { $person3 = htmlentities($_POST['person3']); $starter3 = htmlentities($_POST['starter3']); $maincourse3 = htmlentities($_POST['maincourse3']); $dietary3 = htmlentities($_POST['dietary3']); $guestplaces = 3; } if(isset($_POST['person4'])) { $person4 = htmlentities($_POST['person4']); $starter4 = htmlentities($_POST['starter4']); $maincourse4 = htmlentities($_POST['maincourse4']); $dietary4 = htmlentities($_POST['dietary4']); $guestplaces = 4; } if(isset($_POST['person5'])) { $person5 = htmlentities($_POST['person5']); $starter5 = htmlentities($_POST['starter5']); $maincourse5 = htmlentities($_POST['maincourse5']); $dietary5 = htmlentities($_POST['dietary5']); $guestplaces = 5; } $fullname = htmlentities($_POST['fullname']); $bookdate = htmlentities($_POST['bookdate']); $phonenum = htmlentities($_POST['phonenum']); $emailaddr = htmlentities($_POST['emailaddr']); $content = "Booking Form for {$guestplaces} People.\n Name of Person Booking:\t{$fullname} Date of Booking:\t{$bookdate} Telephone Number:\t{$phonenum} Email Address:\t{$emailaddr}\n Person 1:\t{$person1} Starter:\t{$starter1} Main:\t{$maincourse1} Dietary:\t{$dietary1}\n Person 2:\t{$person2} Starter:\t{$starter2} Main:\t{$maincourse2} Dietary:\t{$dietary2}\n Person 3:\t{$person3} Starter:\t{$starter3} Main:\t{$maincourse3} Dietary:\t{$dietary3}\n Person 4:\t{$person4} Starter:\t{$starter4} Main:\t{$maincourse4} Dietary:\t{$dietary4}\n Person 5:\t{$person5} Starter:\t{$starter5} Main:\t{$maincourse5} Dietary:\t{$dietary5}\n "; $headers = 'From: xxxxx@xxxxxxxx.co.uk' . "\n" . 'Reply-To: ' . "$emailaddr" . "\n" . 'X-Mailer: PHP/' . phpversion(); $mailSent = false; $mailSent = mail($sendto, 'Website Order Form Submission', $content, $headers); if ($mailSent) { $msg = "<div class=\"green\"><p>SUCCESS: Thanks {$fullname} - Your order has been sent.</p></div><br />"; unset($_POST['guestplaces']); unset($_POST['fullname']); unset($_POST['phonenum']); unset($_POST['emailaddr']); unset($_POST['bookdate']); unset($_POST['person']); unset($_POST['starter']); unset($_POST['maincourse']); unset($_POST['dietary']); unset($guestplaces); unset($fullname); unset($phonenum); unset($emailaddr); unset($bookdate); unset($person); unset($starter); unset($maincourse); unset($dietary); unset($peoplehere); } } ?> Thanks in advanced for any help able to be offered!
May 18, 201214 yr just for reference to save code u could do this for the amount of people using a for loop <select name="guests" id="guests"> <?php for($i = 1; $i<= 5; $i++){ echo "<option value='$i'>$i</option>"; } ?> </select>
May 18, 201214 yr why dont you just use another loop? if(isset($_POST["submitorder"])) { $guests = $_POST['guests']; $sendto = $_POST['sendto']; $fullname = htmlentities($_POST['fullname']); $bookdate = htmlentities($_POST['bookdate']); $phonenum = htmlentities($_POST['phonenum']); $emailaddr = htmlentities($_POST['emailaddr']); $content = "Booking Form for {$guests} People.\n Name of Person Booking:\t{$fullname} Date of Booking:\t{$bookdate} Telephone Number:\t{$phonenum} Email Address:\t{$emailaddr}\n "; for ($i = 1; $i <= $guests; $i++){ if (isset($_POST['person'$i])) { $content .= htmlentities($_POST['person'$i]); $content .= htmlentities($_POST['starter'$i]); $content .= htmlentities($_POST['maincourse'$i]); $content .= htmlentities($_POST['dietary'$i]); } sorry - got the concatenator operator wrong, learning javascript at the moment Edited May 18, 201214 yr by gadgetgirl
Create an account or sign in to comment