Stop Receiving Blank Emails!

Discussion in 'PHP' started by gobbly2100, Sep 11, 2007.

  1. #1
    Hello,

    How can I make it so my script will not allow people to hit submit until the have filled in the required field/s because if you hit submit without entering anything I get a blank email which is a little annoying.

    HTML
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p>Name:<br>
    <input name="name" type="text" />
     </p>
    <p>Email:<br>
    <input name="email" type="text" />
     </p>
    <p>Subject:<br>
    <input name="subject" type="text" />
     </p>
    <p>Message:<br>
    <textarea name="message" rows="6"></textarea>
     <br />
    <input type="submit" value="Send" /><input type="hidden" name="do" value="send" /><input type="reset" value="Reset" />
     </p></form>
    Code (markup):
    PHP
    <?php
    if($_POST['do']=="send") {
    $recipient="Email"; // Set your email here //
    $subject=$_POST['subject'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $message = "Name: {$name}\n\nMessage:\n{$_POST['message']}";
    $formsend=mail("$recipient", "$subject", "$message", "From: $name ($email)\r\nReply-to:$email");
    echo("<p>Your message was successfully sent!</p>");
    }
    ?>
    Code (markup):
    Thanks in advance!
     
    gobbly2100, Sep 11, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    A very simple way :D

    <?php
    $errors = array();
    if($_POST['do']=="send") {
      $recipient="Email"; // Set your email here //
      if (!$subject=$_POST['subject']) $errors[] = 'Subject required';
      if (!$name=$_POST['name']) $errors[] = 'Name required';
      if (!$email=$_POST['email']) $errors[] = 'Email required';
      if (!$message=$_POST['message']) $errors[] = 'Message required';
      if (!$errors) {
        $message = "Name: {$name}\n\nMessage:\n{$message}";
        $formsend=mail("$recipient", "$subject", "$message", "From: $name ($email)\r\nReply-to:$email");
        echo("<p>Your message was successfully sent!</p>");
      }
    }
    ?>
    PHP:
    And in your HTML:
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php if ($errors) echo '<p>The following errors occurred: <br />' . join('<br />', $errors) . '</p>'; ?>
    <p>Name:<br>
    <input name="name" type="text" />
     </p>
    <p>Email:<br>
    <input name="email" type="text" />
     </p>
    <p>Subject:<br>
    <input name="subject" type="text" />
     </p>
    <p>Message:<br>
    <textarea name="message" rows="6"></textarea>
     <br />
    <input type="submit" value="Send" /><input type="hidden" name="do" value="send" /><input type="reset" value="Reset" />
     </p></form>
    Code (markup):
     
    krt, Sep 11, 2007 IP
  3. gobbly2100

    gobbly2100 Banned

    Messages:
    906
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Awesome, thank you!
     
    gobbly2100, Sep 12, 2007 IP
  4. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    It would also a good practice to have both sides validation. krt proposal is correct, but i would add Javascript validations to avoid useless execution of the server script.
     
    webrickco, Sep 12, 2007 IP