mikevarela Posted May 28, 2009 Share Posted May 28, 2009 Hi, I'm creating a form for a client to submit name and songs for their wedding that I'm gonna DJ. I have text boxes for each of the major people... bride, groom, bestman etc. I'm having trouble figuring out how to build the message body of the email, so that it send the info that the user submitted, and if they didn't submit anything in certain boxes, it doesn't email me the details of those. For a couple of text boxes I'm fine, but there's about 20 here. Looking to possibly create and array of values from the boxes, but can't wrap my head around it. here's some code from my form <tr> <td><label for="bridesmaid_1">1st Bridesmaid</label></td> <td><input type="text" name="bridesmaid_1" id="bridesmaid_1" /></td> </tr> <tr> <td><label for="groomsman_1">1st Groomsman</label></td> <td><input type="text" name="groomsman_1" id="groomsman_1" /></td> </tr> <tr> <td><label for="bridesmaid_2">2nd Bridesmaid</label></td> <td><input type="text" name="bridesmaid_2" id="bridesmaid_2" /></td> </tr> <tr> <td><label for="groomsman_2">2nd Groomsman</label></td> <td><input type="text" name="groomsman_2" id="groomsman_2" /></td> </tr> <tr> <td><label for="bridesmaid_3">3rd Bridesmaid</label></td> <td><input type="text" name="bridesmaid_3" id="bridesmaid_3" /></td> </tr> and here's the php i have // initialize a variable to put any errors we encounter into an array $errors = array(); // test to see if the form was actually posted from our form $page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; if (!ereg($page, $_SERVER['HTTP_REFERER'])) $errors[] = "Invalid referer\n"; // check to see if a name was entered if (!$_POST['name']) // if not, add that error to our array $errors[] = "Your name is required"; // check to see if a subject was entered if (!$_POST['bride']) // if not, add that error to our array $errors[] = "Bride's name is required"; // check to see if a message was entered if (!$_POST['groom']) // if not, add that error to our array $errors[] = "Groom's name is required"; if (!$_POST['maid_of_honor']) // if not, add that error to our array $errors[] = "Maid of Honors name is required"; if (!$_POST['best_man']) // if not, add that error to our array $errors[] = "Best Man's name is required"; // if there are any errors, display them if (count($errors)>0){ echo "<strong>ERROR:<br>\n"; foreach($errors as $err) echo "$err<br>\n"; } else { // no errors, so we build our message $recipient = '***********'; $from = stripslashes($_POST['name']); $subject = 'Wedding List'; $msg = "Message sent by $from\n"; $msg.= "\nBride: ".stripslashes($_POST['bride']); $msg.= "\nGroom: ".stripslashes($_POST['groom']); $msg.= "\nMaid of Honor: ".stripslashes($_POST['maid_of_honor']); $msg.= "\nBest Man: ".stripslashes($_POST['best_man']); $msg.= "\nExtra Notes: ".stripslashes($_POST['extra_notes']); } if (mail($recipient,$subject,$msg)){ echo "<p>Thanks for filling out the form!</p>"; echo nl2br($msg); echo "<p><b>PRINT THIS PAGE FOR YOUR RECORDS</b></p>"; } else echo "An unknown error occurred."; } Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/ Share on other sites More sharing options...
Axeia Posted May 29, 2009 Share Posted May 29, 2009 Are you after something like, $fields = array( "bridesmaid_1" => "1st Bridesmaid", "groomsman_1" => "1st Groomsman", "bridesmaid_2" => "2nd Bridesmaid", "groomsman_2" => "2nd Groomsman", ); <table> <?php foreach( $fields as $id => $field ) { echo "<tr> <td><label for='$id'>$field</td> <td><input type='text' name='$id' id='$id' /></td> </tr>"; } ?> </table> On the receiving page you could also validate the information with something like, foreach($fields as $id => $field) { switch( $_POST[$id] ) { case 'bridesmaid_1': case 'groomsman_1': errors[] = $field." is required!"; } } All untested and written in my browser, typo's/syntax error imminent Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-844765 Share on other sites More sharing options...
mikevarela Posted May 29, 2009 Author Share Posted May 29, 2009 Wow, i think this might work. Thanks so much. I'm still a little shaky on my own two feet and this helps alot. by the way, should i process on the same page or send it all somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845045 Share on other sites More sharing options...
Hybride Posted May 29, 2009 Share Posted May 29, 2009 That's a personal preference I think. I usually put my process on the same page. Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845079 Share on other sites More sharing options...
Axeia Posted May 29, 2009 Share Posted May 29, 2009 Yeah that one is up to you, I also prefer to have it on the same page as with the code above you use the input array for the validation as well, so it's basically #non functional example code $array = array(); if( POST ) { //output form based on $array } else { //validate based on $array; } And by having it on the some page you don't need to include it from elsewhere or do other strange things like resorting to global variables. Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845105 Share on other sites More sharing options...
mikevarela Posted May 29, 2009 Author Share Posted May 29, 2009 I had a few issues with the validation... Can you run me through that once again. I have the top of the page checking to see if $_POST[submit] has been hit, if not then the form display, else we go through the validation. here's what i have now, I tried to change it to yours but kept getting errors <?php include('wedding_includes/header.inc.php'); ?> <?php if ($_SERVER['REQUEST_METHOD'] != 'POST'){ $me = $_SERVER['PHP_SELF']; // Form Fields Array $fields = array( "name" => "Your Name", "bride" => "Bride", "groom" => "Groom", "bridesmaid_1" => "1st Bride's Maid", "groomsman_1" => "1st Groomsman", "bridesmaid_2" => "2nd Bride's Maid", "groomsman_2" => "2nd Groomsman", "bridesmaid_3" => "3rd Bride's Maid", "groomsman_3" => "3rd Groomsman", "bridesmaid_4" => "4th Bride's Maid", "groomsman_4" => "4th Groomsman", "bridesmaid_5" => "5th Bride's Maid", "groomsman_5" => "5th Groomsman", "bride_mother" => "Bride's Mother", "bride_father" => "Bride's Father", "groom_mother" => "Groom's Mother", "groom_father" => "Groom's Father", "bride_groom_dance" => "Bride / Groom Dance", "father_daughter_dance" => "Father / Daughter Dance", "mother_groom_dance" => "Mother / Son Dance", "cake_song" => "Cake Cutting Song", "entrance_song" => "Entrance Song", "last_song" => "Last Song", "email" => "Your Email" ); ?> <p>Please fill in the information below.</p> <form action="<?php echo $me;?>" method="post"> <table id="wedding_dance_form" align="center" cellpadding="2"> <?php foreach( $fields as $id => $field ) { echo "<tr> <td><label for='$id'>$field</td> <td><input type='text' name='$id' id='$id' /></td> </tr>"; } ?> <tr> <td><label for="extra_notes">Extra Note's</label></td> <td><textarea name="extra_notes" id="extra_notes" cols="50" rows="8"></textarea></td> </tr> <tr> <td><label for="submit">Submit List</label></td> <td><input type="submit" name="submit" id="submit" value="Send" /></td> </tr> </table> </form> <?php }else{ // initialize a variable to put any errors we encounter into an array $errors = array(); // test to see if the form was actually posted from our form $page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; if (!ereg($page, $_SERVER['HTTP_REFERER'])) $errors[] = "Invalid referer\n"; // check to see if a name was entered if (!$_POST['name']) // if not, add that error to our array $errors[] = "Your name is required"; // check to see if a subject was entered if (!$_POST['bride']) // if not, add that error to our array $errors[] = "Bride's name is required"; // check to see if a message was entered if (!$_POST['groom']) // if not, add that error to our array $errors[] = "Groom's name is required"; if (!$_POST['maid_of_honor']) // if not, add that error to our array $errors[] = "Maid of Honors name is required"; if (!$_POST['best_man']) // if not, add that error to our array $errors[] = "Best Man's name is required"; if (!$_POST['bride_groom_dance']) // if not, add that error to our array $errors[] = "Bride/Groom dance is required"; if (!$_POST['cake_song']) // if not, add that error to our array $errors[] = "Cake Cutting song is required"; if (!$_POST['email']) // if not, add that error to our array $errors[] = "Your email is required"; // if there are any errors, display them if (count($errors)>0){ echo "<strong class=\"redtext\">ERROR:<br>\n"; foreach($errors as $err) echo "$err<br>\n"; }else{ // no errors, so we build our message $recipient = 'mike@nuancetone.com'; $from = "Name: ".stripslashes($_POST['name'])."| Email: ".$_POST['email']; $subject = 'Wedding List'; $msg = 'Submitted Responses'; foreach($_POST as $key => $val){ if (is_array($val)){ $msg.="Item: $key\n"; foreach($val as $v){ $v = stripslashes($v); $msg.=" $v\n"; } }else{ $val = stripslashes($val); $msg.="$key: $val\n"; } } } if (mail($recipient,$subject,$msg)){ echo "<p>Thanks for filling out the form!</p>"; echo nl2br($msg); echo "<p><b>PRINT THIS PAGE FOR YOUR RECORDS</b></p>"; }else echo "An unknown error occurred."; } ?> <?php include('wedding_includes/footer.inc.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845329 Share on other sites More sharing options...
Axeia Posted May 29, 2009 Share Posted May 29, 2009 Quite easy ones to spot tbh. If you fill in everything you get the following errors: Maid of Honors name is required Best Man's name is required An unknown error occurred. So ctrl+f (search) in your code for "maid of", which leads to if (!$_POST['maid_of_honor']) // if not, add that error to our array $errors[] = "Maid of Honors name is required"; Which means you should have a input field whose name is "maid_of_honor" as well (like in the post) ctrl+f for "maid_of_honor" and you'll notice the focus wont change as it only occurs once in the file. And basically every time you write something like this: // check to see if a name was entered if (!$_POST['name']) // if not, add that error to our array $errors[] = "Your name is required"; // check to see if a subject was entered if (!$_POST['bride']) // if not, add that error to our array $errors[] = "Bride's name is required"; // check to see if a message was entered if (!$_POST['groom']) // if not, add that error to our array $errors[] = "Groom's name is required"; if (!$_POST['maid_of_honor']) // if not, add that error to our array $errors[] = "Maid of Honors name is required"; if (!$_POST['best_man']) // if not, add that error to our array $errors[] = "Best Man's name is required"; if (!$_POST['bride_groom_dance']) // if not, add that error to our array $errors[] = "Bride/Groom dance is required"; if (!$_POST['cake_song']) // if not, add that error to our array $errors[] = "Cake Cutting song is required"; if (!$_POST['email']) // if not, add that error to our array $errors[] = "Your email is required"; You should ask yourself if what you're doing is a good way to do what you're doing. Usually if you got a lot of similar code it's indication that there is a better way to do the same thing. Via a function or an array. Here's your form, but fixed: <?php $fields = array( "name" => "Your Name", "bride" => "Bride", "groom" => "Groom", "bridesmaid_1" => "1st Bride's Maid", "groomsman_1" => "1st Groomsman", "bridesmaid_2" => "2nd Bride's Maid", "groomsman_2" => "2nd Groomsman", "bridesmaid_3" => "3rd Bride's Maid", "groomsman_3" => "3rd Groomsman", "bridesmaid_4" => "4th Bride's Maid", "groomsman_4" => "4th Groomsman", "bridesmaid_5" => "5th Bride's Maid", "groomsman_5" => "5th Groomsman", "bride_mother" => "Bride's Mother", "bride_father" => "Bride's Father", "groom_mother" => "Groom's Mother", "groom_father" => "Groom's Father", "bride_groom_dance" => "Bride / Groom Dance", "father_daughter_dance" => "Father / Daughter Dance", "mother_groom_dance" => "Mother / Son Dance", "cake_song" => "Cake Cutting Song", "entrance_song" => "Entrance Song", "last_song" => "Last Song", "email" => "Your Email" ); if ($_SERVER['REQUEST_METHOD'] != 'POST'){ $me = $_SERVER['PHP_SELF']; // Form Fields Array ?> <p>Please fill in the information below.</p> <form action="<?php echo $me;?>" method="post"> <table id="wedding_dance_form" align="center" cellpadding="2"> <?php foreach( $fields as $id => $field ) { echo "<tr> <td><label for='$id'>$field</td> <td><input type='text' name='$id' id='$id' /></td> </tr>"; } ?> <tr> <td><label for="extra_notes">Extra Note's</label></td> <td><textarea name="extra_notes" id="extra_notes" cols="50" rows="8"></textarea></td> </tr> <tr> <td><label for="submit">Submit List</label></td> <td><input type="submit" name="submit" id="submit" value="Send" /></td> </tr> </table> </form> <?php }else{ print_r( $_POST ); //Say hello to you new best friend, print_r // initialize a variable to put any errors we encounter into an array $errors = array(); // test to see if the form was actually posted from our form $page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; if (!ereg($page, $_SERVER['HTTP_REFERER'])) $errors[] = "Invalid referer\n"; foreach($fields as $id => $field) { if(!$_POST[$id]) { switch( $id ) //Below are the REQUIRED cases { case 'name': case 'bride': case 'groom': case 'maid_of_honor': case 'best_man': case 'bride_groom_dance': case 'cake_song': case 'email': $errors[] = $field." is required!"; } } } // if there are any errors, display them if (count($errors)>0){ echo "<strong class=\"redtext\">ERROR:<br>\n"; foreach($errors as $err) echo "$err<br>\n"; }else{ echo "Bingo!"; // no errors, so we build our message $recipient = 'mike@nuancetone.com'; $from = "Name: ".stripslashes($_POST['name'])."| Email: ".$_POST['email']; $subject = 'Wedding List'; $msg = 'Submitted Responses'; foreach($_POST as $key => $val){ if (is_array($val)){ $msg.="Item: $key\n"; foreach($val as $v){ $v = stripslashes($v); $msg.=" $v\n"; } }else{ $val = stripslashes($val); $msg.="$key: $val\n"; } } } //if (mail($recipient,$subject,$msg)){ // echo "<p>Thanks for filling out the form!</p>"; // echo nl2br($msg); // echo "<p><b>PRINT THIS PAGE FOR YOUR RECORDS</b></p>"; //}else { // echo "An unknown error occurred."; } ?> I added a print_r so you can see what's going on and commented out your mail function as I don't want to send spam Another necessary change was moving the $fields array OUTSIDE the if, since the values are needed in the switch in the else. So to grant them both access to it , it was place outside them. Quote Link to comment https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.