How do I make a mail form print an error message if something is not filled out?

258 views Asked by At

Good evening, for my website I am using a mail form. Apparently it is saying that the message was sent, eve though nothing was filled out. I also donĀ“t get an e-mail then. What do I have to include in the code, so that it is checked before sending, that the email, subject and message is filled out? Thank you for you help!

     <?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
          {
          //send email
          $email = $_REQUEST['email'] ;
          $subject = $_REQUEST['subject'] ;
          $message = $_REQUEST['message'] ;
          mail("***.****@gmail.com", $subject,
          $message, "From:" . $email);
          echo "Thank you for using our mail form. We will reply as soon as possible.";
          }
    else
          {
          echo "<form method='post' action='mailform.php'>
          Email: <br> <input name='email' type='text'><br>
          Subject: <br> <input name='subject' type='text'><br>
          Message:<br>
          <textarea name='message' rows='15' cols='40'>
          </textarea><br>
          <input type='submit'>
          </form>";
          }
    ?>
2

There are 2 answers

0
Aryeh Armon On
<?php
if (isset($_REQUEST['email']) && isset($_REQUEST['message']) && isset($_REQUEST['subject']))
//if "email" is filled out, send email
      {
      //send email
      $email = $_REQUEST['email'] ;
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail("***.****@gmail.com", $subject,
      $message, "From:" . $email);
      echo "Thank you for using our mail form. We will reply as soon as possible.";
      }
else
      {
      if (isset($_REQUEST['submit']){
          echo 'display error here';
       }
      echo "<form method='post' action='mailform.php'>
      Email: <br> <input name='email' type='text'><br>
      Subject: <br> <input name='subject' type='text'><br>
      Message:<br>
      <textarea name='message' rows='15' cols='40'>
      </textarea><br>
      <input type='submit'>
      </form>";
      }
?>
0
Anonymous On

You need to check for $_REQUEST['email'] != '' instead of isset($_REQUEST['email']) and do the same for the other variables.
The function isset() checks to see if the variable has any value including if the variable was defined as nothing.