Jump to content

Recommended Posts

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.";
}

Link to comment
https://forums.phpfreaks.com/topic/160056-form-to-email-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-844765
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845105
Share on other sites

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'); ?>	 
       
       
       

Link to comment
https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845329
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/160056-form-to-email-help/#findComment-845354
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.