PHP send mail null field questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbatestblrock
    New Member
    • Sep 2007
    • 164

    PHP send mail null field questions

    Ok, I will be the first to admit I am not too savvy with PHP. That being said, I was hoping for some quick help, I dont think it is too difficult, but I cant seem to come up with search results to find my answer.

    My html is posting just fine to the php script, it emails, and all is well except for one thing. I have fields in the form that do not need to be filled out, and there are about 40 fields. When it gets mailed to me and if they dont fill in lets say, 30 of the 40+ fields, the entire thing gets emailed to me with no info put in those blank spots but it still has its labled that I provided it so it looks like:

    Addreess:
    Phone:

    With 40+ of these its gets kind of hard to find the information they provided that I need, and I am worried Ill skip over something.

    So the way I have it setup is just not working for me and I was wondering if there is something relatively simple I could put in the PHP script for (if the field is blank display nothing) I dont know if this will work with how I have it setup (its pretty basic)

    But I have provided a SLIMMED down portion of the code that should answer any questions one may have about it.

    So thank you for any advice you can give! Have a great day.. or night depending on where your at!

    [PHP]<?php
    $FirstName = $_REQUEST['FirstName'];
    $LastName = $_REQUEST['LastName'];
    $City = $_REQUEST['City'];
    $State = $_REQUEST['State'];
    $Zip = $_REQUEST['Zip'];
    $HomePhone = $_REQUEST['HomePhone'];
    $CellPhone = $_REQUEST['CellPhone'];
    $Email = $_REQUEST{'Emai l'} ;
    $Comments = $_REQUEST{'Comm ents'} ;

    mail( "mbates@tblrock .com,", "Table Rock Restaurants Form Submission",
    "First Name: $FirstName\n
    Last Name: $LastName\n
    City: $City\n
    State: $State\n
    Zip: $Zip\n
    Home Phone: $HomePhone\n
    Cell Phone: $CellPhone\n
    Email Address: $Email\n
    Comments from $FirstName $LastName : $Comments",


    "From: user@website.co m" );
    header( "Location: http://www.website.com " );
    ?>[/PHP]
  • aktar
    New Member
    • Jul 2006
    • 105

    #2
    [PHP]$message = NULL;

    foreach ($_REQUEST as $field_name => $value){

    if (!empty($value) ) $message =. "$field_nam e = $value<br />\n\r";
    }

    $to = "mbates@tblrock .com";
    $subject = "Table Rock Restaurants Form Submission";
    $header = "From: user@website.co m";

    mail($to ,$subject, $message, $header);
    [/PHP]

    Comment

    • mbatestblrock
      New Member
      • Sep 2007
      • 164

      #3
      Originally posted by aktar
      [PHP]$message = NULL;

      foreach ($_REQUEST as $field_name => $value){

      if (!empty($value) ) $message =. "$field_nam e = $value<br />\n\r";
      }

      $to = "mbates@tblrock .com";
      $subject = "Table Rock Restaurants Form Submission";
      $header = "From: user@website.co m";

      mail($to ,$subject, $message, $header);
      [/PHP]
      Great I am going to try this out on monday, I will post back with how she goes! Thanks a ton!

      Comment

      • mbatestblrock
        New Member
        • Sep 2007
        • 164

        #4
        Originally posted by mbatestblrock
        Great I am going to try this out on monday, I will post back with how she goes! Thanks a ton!

        Okay, So is that script supposed to work JUST like that. I changed it to that and I get nothing when the form is submitted. It takes me to the sendmail.php page and it is just blank, and I do not receive the email.

        Looking at the script it seems like it should work without any tweaking or without defining any of the form variables??

        Again.. me and php dont get along. If I need to tailor this script more I would love to hear what I need to do. Ill be messing it with it until then!

        THANKS!

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Again.. me and php dont get along. If I need to tailor this script more I would love to hear what I need to do
          We are not in the habit of writing someone else's PHP scripts.

          My advice to you is: follow some PHP tutorials. do some PHP practicums, then adapt your script and, if you still have problems, come back here.

          Ronald

          Comment

          • mbatestblrock
            New Member
            • Sep 2007
            • 164

            #6
            Originally posted by ronverdonk
            We are not in the habit of writing someone else's PHP scripts.

            My advice to you is: follow some PHP tutorials. do some PHP practicums, then adapt your script and, if you still have problems, come back here.

            Ronald
            Yes I know, I am not asking you to write my script. my whole script is quite lengthy, I trimmed the entire thing down just to get some advice. I got it up and running from following a tutorial. And from searching around I cannot figure out how to adapt my script to make it do the null field thing I want it to. My search results are just coming back with stuff that isnt right at all. So my apologies extend to those who feel I am being a prick and not even attempting to try and solve it by myself.. because thats not the case.

            Comment

            • aktar
              New Member
              • Jul 2006
              • 105

              #7
              Hi Mbates,


              if you are going to have the form processor in the same page as the form itself, then you must tell php to try to process the form ONLY if certain conditions are met.

              In the example below our condition will be : if form element "submit" exists

              [PHP]<?php

              if (isset($_REQUES T['submit'])){


              $message = NULL;

              foreach ($_REQUEST as $field_name => $value){

              if (!empty($value) ) $message =. "$field_nam e = $value<br />\n\r";
              }

              $to = "mbates@tblrock .com";
              $subject = "Table Rock Restaurants Form Submission";
              $header = "From: user@website.co m";

              mail($to ,$subject, $message, $header);

              header("your_th ank_you_page.ht ml");
              }
              ?>

              <form id="form1" name="form1" method="post" action="">
              <input type="text" name="textfield " id="textfield" />
              <input type="text" name="textfield 2" id="textfield2 " />
              <input type="text" name="textfield 3" id="textfield3 " />
              <input type="submit" name="submit" id="submit" value="Submit" />
              </form>[/PHP]

              If the condition is not met then PHP will ignore the php bit and form will be displayed. But if the condition is met then the user will be re-directed to a thank you page.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Hi.

                Just a thought...

                In this case, using the $_REQUEST array is a bad idea.
                You are using the data rather blindly here. Suppose I were to add this to the URL:
                Code:
                ?j=<script>location.href='www.example.com/downloadEbilViruses.html;</script>
                I cud have you re-directed just about anywhere :)

                Use the $_POST array instead. That way people will actually have to do some work to send you viruses ;)

                Running the input through a few cleanup functions wouldn't be a bad idea either.
                The htmlentities function for example.

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  Here's how I would do it:

                  [code=php]<?php
                  // Checks if this is the right form (I suggest only have required field here, so say FirstName, LastName, Email and Comments):
                  if ( $_SERVER['REQUEST_METHOD '] == 'POST' && isset($_POST['FirstName']) && isset($_POST['LastName']) && isset($_POST['Email']) && isset($_POST['Comments']) )
                  {

                  if ( $_POST['FirstName'] == "" || $_POST['LastName'] == "" || $_POST['Email']] == "" || $_POST['Comments'] == "" )
                  {
                  echo ('You have not entered the required information');
                  } else {

                  $FirstName = $_POST['FirstName'];
                  $LastName = $_POST['LastName'];
                  $City = $_POST['City'];
                  $State = $_POST['State'];
                  $Zip = $_POST['Zip'];
                  $HomePhone = $_POST['HomePhone'];
                  $CellPhone = $_POST['CellPhone'];
                  $Email = $_POST['Email'] ; //You know you had { instead of [ ?
                  $Comments = $_POST['Comments'] ;

                  //Now for the message, you only need to check the ones which are optional
                  $message = "
                  First Name: $FirstName\n
                  Last Name: $LastName\n
                  if ($City != NULL) {City: $City\n}
                  if ($State != NULL) {State: $State\n}
                  if ($Zip != NULL) {Zip: $Zip\n}
                  if ($HomePhone != NULL) {Home Phone: $HomePhone\n}
                  if ($CellPhone != NULL) {Cell Phone: $CellPhone\n}
                  Email Address: $Email\n
                  Comments from $FirstName $LastName : $Comments"


                  mail( "mbates@tblrock .com,", "Table Rock Restaurants Form Submission",
                  $message,"From: user@website.co m" );
                  }
                  header( "Location: http://www.website.com " );
                  }
                  ?>[/code]

                  Or something like that. Maybe I have made a mistake, but it should give you some more insight into what php can do.

                  [EDIT]Aktar's foreach loop is a better way of doing this, but mine is a longer and in some senses simpler, so you might follow it a bit better.
                  Last edited by TheServant; Apr 8 '08, 03:29 AM. Reason: Page error and I didn't see all the previous replies.

                  Comment

                  Working...