Results 1 to 4 of 4

Thread: [RESOLVED] PHP-Don't include blank form fields when emailed

Threaded View

  1. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP-Don't include blank form fields when emailed

    since you're not actually doing any validation (you simply create variables out of the $_POST globals, but do not actually make sure these have a value), we could simply loop through the $_POST array and only add onto $email_message when the value of that index is not empty. in the last piece of code you posted, this would replace all of the code you have from the beginning until the "create email headers comment":
    PHP Code:
    // start the message as an empty string
    $email_message "";

    // loop through the POST array getting the index ($key) and the value ($value):
    foreach($_POST as $key => $value){

      
    // is this value not empty?
      
    if(!empty($value)){

        
    // it is not empty. add onto the $email_message
        
    $email_message .= "{$key}: " clean_string($value) . "\r\n";

      }

    // end foreach 
    this will give you the basic functionality of what you want. however, we end up with a slight problem. this will produce the following results (as an example):
    Code:
    attendance: ...
    guest1-fn: ...
    guest1-ln: ...
    guest2-fn: ...
    guest2-fn: ...
    etc.
    this is usable (because you created this form and you understand it), but it may not be desirable. because we are using the index ($key) in our foreach loop, we are getting the name of the <input> fields on your form, not the labels (PHP has no idea what you labeled your fields, it only knows the name attribute as that is what gets sent to the server). we can make a work around for this by creating an associative array that will hold reference to all of the labels that we want. here is an example of the first few indexes ($keys) and how they would reference the label that would show up in the email:
    PHP Code:
      $labels = array(
                      
    'attendance' => 'Attendance',
                      
    'guest1-fn' => 'Guest 1 First Name',
                      
    'guest1-ln' => 'Guest 1 Last Name',
                      
    'other_allergies1' => 'Guest 1 Allergies',
                      ...
                      
    'guest6-fn' => 'Guest 6 First Name',
                      
    'guest6-ln' => 'Guest 6 Last Name',
                      
    'other_allergies6' => 'Guest 6 Allergies',
                      
                      
    'hotel' => 'Hotel',
                      
    'transportation' => 'Transportation',
                      ...
                      ); 
    I hope that format makes sense to you. as you can hopefully see, we are using the name of the field on the form as the index (or key) to the array, which is associated with the label for that particular index. I only filled in enough to hopefully give you the idea, however. then, we can modify the code from above to incorporate our new array, which should give us a final product that looks a little like this:
    PHP Code:
      // our array of form labels
      
    $labels = array(
                      
    'attendance' => 'Attendance',
                      
    'guest1-fn' => 'Guest 1 First Name',
                      
    'guest1-ln' => 'Guest 1 Last Name',
                      
    'other_allergies1' => 'Guest 1 Allergies',
                      ...
                      
    'guest6-fn' => 'Guest 6 First Name',
                      
    'guest6-ln' => 'Guest 6 Last Name',
                      
    'other_allergies6' => 'Guest 6 Allergies',
                      
                      
    'hotel' => 'Hotel',
                      
    'transportation' => 'Transportation',
                      ...
                      );

      
    // start the message as an empty string
      
    $email_message "";

      
    // loop through the POST array getting the index ($key) and the value ($value):
      
    foreach($_POST as $key => $value){

        
    // is this value not empty?
        
    if(!empty($value)){

          
    // it is not empty. add onto the $email_message
          
    $email_message .= (isset($labels[$key]) ? $labels[$key] : $key) . ": " clean_string($value) . "\r\n";

        }

      } 
    // end foreach 
    the new code in question is below, which is called a ternary operation (or a shorthand if statement):
    PHP Code:
    (isset($labels[$key]) ? $labels[$key] : $key
    that code basically acts the same as:
    PHP Code:
    if(isset($labels[$key])){
      
    $email_message .= $labels[$key];
    }else{
      
    $email_message .= $key;

    the ternary operation saves us space and time because it is inline, though it may not be easily understood at first glance. if the second example makes more sense to you, I implore you to use that instead! whichever you choose to use, this little if statement basically makes sure that a label is associated with it in the $labels array before adding it to the $email_message; if a label is not associated with it, then we fall back to using just the index ($key) instead to be able to show something to the person receiving the email.

    so. that's a fairly basic way of doing what you'd like to do. the next step would be to stop iteration from the processing and outputting parts of your form and combine them. basically, instead of creating your form statically, you would create an array that held all of your form elements' properties (what type of input field, the name, the label, etc), and you would look through that to both build and process your form. however, that would take a lot more work!

    anyhow, I hope this had made some sense to you. if not, feel free to ask more questions.
    Last edited by kows; Apr 19th, 2010 at 12:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width