How do I exlude an empty field from being submitted?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moishy
    New Member
    • Oct 2006
    • 104

    #1

    How do I exlude an empty field from being submitted?

    I get the form emailed to me, but I get the following:

    Product_01:
    Product_02: 1
    Product_03:
    Product_04: 4
    Product_05:

    So is there a way that I'll only see:

    Product_02: 1
    Product_04: 4

    Thanx.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What form? What emailed to me? What is your problem?
    If you are double posting this thread will be closed.

    Ronald :cool:

    Comment

    • moishy
      New Member
      • Oct 2006
      • 104

      #3
      What I'm saying is:
      I have a form on my website which you fill out your contact info, and your desired quantity of product.

      the form is of this type:

      Name: _____
      Phone: _____
      Email: _____

      ___ Product 1
      ___ Product 2
      ___ Product 3

      So I've preveously posted a question about not filling out the required fields, which will earn you to be sent to the HTTP_REFFER.

      So that's solved.

      So if somebody submits a form, neglecting to fill out his name, the page will refresh, and not submit.

      But my problem now is:
      that when a costomer is buying 1 of Product_2, The email mailed to me upon submit will say:

      Name: John Doe
      Phone: 1234-567-8910
      Email: me@mydomain.com
      Product_01:
      Product_02: 1
      Product_03:

      But I wanted to just remain with:

      Name: John Doe
      Phone: 1234-567-8910
      Email: me@mydomain.com
      Product_02: 1

      So how do I omit an empty field from being included in the email?

      Did I clarify myself?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        We can only deduct that from the code that builds and fills the email message with the values. So post that code here and we'll have a look.

        Ronald :cool:

        Comment

        • vssp
          Contributor
          • Jul 2006
          • 268

          #5
          Any possibility Are u post the code?

          Comment

          • bevort
            New Member
            • Jul 2006
            • 53

            #6
            As soon as you press the submit button in a form the complete form is send to the server. thus empty fields are also send and will show up as empty in your email you get from this form.
            If you do not want this, send the form to the server (to itself) and check in an IF ($submit) {} each var and make a mail to yourself using
            mail ( string to, string subject, string message [, string additional_head ers [, string additional_para meters]] )
            Read more explanation on this on php.net

            Comment

            • moishy
              New Member
              • Oct 2006
              • 104

              #7
              Here's the code:

              [PHP]<?php

              /*
              Enter the email address below to send the form to:
              */

              $my_email = "me@mydomain.co m";


              // This line prevents values being entered in a URL

              if ($_SERVER['REQUEST_METHOD '] != "POST"){exi t;}

              // Check referrer is from same site.

              if(!(isset($_SE RVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVE R['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){print "Please enable referrer logging to use this contact form. Your message was not sent."; exit;}

              // Describe function to check for new lines.

              function new_line_check( $a)
              {

              if(preg_match(' `[\r\n]`',$a)){header( "location: $_SERVER[HTTP_REFERER]");exit;}

              }

              new_line_check( $_POST['Name']);

              // Check for disallowed characters in the Name and Email fields.

              $disallowed_nam e = array(':',';',' "','=','(',')', '{','}','@');

              foreach($disall owed_name as $value)
              {

              if(stristr($_PO ST['Name'],$value)){heade r("location: $_SERVER[HTTP_REFERER]");exit;}

              }

              new_line_check( $_POST['Email']);

              $disallowed_ema il = array(':',';'," '",'"','=','(', ')','{','}');

              foreach($disall owed_email as $value)
              {

              if(stristr($_PO ST['Email'],$value)){heade r("location: $_SERVER[HTTP_REFERER]");exit;}

              }

              $message = "";



              # This Block I added thanks to your help
              # Avoid empty message - If empty - Go back

              if(empty($_POST['Name'])){header("loca tion: $_SERVER[HTTP_REFERER]");exit;


              }else{

              }


              # I use this Block not to email me the submit button
              # Fields to Avoid
              $ex=array();
              $ex[]="submit,submit 2"; # Where submit is the name of the submit button.

              # iterate the $_POST array
              foreach ($_POST as $key=>$value) {
              if (!in_array($key , $ex)) {
              $message.=$key. ": " . $value . "\n";
              }
              }

              $message = stripslashes($m essage);

              $subject = "e-Form";
              $headers = "From: " . $_POST['Email'] . "\n" . "Return-Path: " . $_POST['Email'] . "\n" . "Reply-To: " . $_POST['Email'] . "\n";

              mail($my_email, $subject,$messa ge,$headers);



              ?>
              [/PHP]

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Replace the # iterate the $_POST array with:
                [php]# iterate the $_POST array
                foreach ($_POST as $key=>$value) {
                if (!in_array($key , $ex) AND $value > "" AND $value != 0) {
                $message.=$key. ": " . $value . "\n";
                }
                } [/php]

                Ronald :cool:

                Comment

                • moishy
                  New Member
                  • Oct 2006
                  • 104

                  #9
                  Thanks Ronald!
                  I based my code on what you wrote:
                  [PHP]# iterate the $_POST array
                  foreach ($_POST as $key=>$value) {
                  if (!in_array($key , $ex) AND $value != "") {
                  $message.=$key. ": " . $value . "\n";}
                  } [/PHP]

                  Comment

                  Working...