0

I am trying to create a form that checks and validates name, email. But I can't see any error messages. I don't know a lot of PHP, can't say that I even know the basics.

Here is the code:

<iframe name="formDestination" class="nnn"></iframe>
<div class="container33">
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post"  target="formDestination">
            <label for="fname">Full Name</label>
            <input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span>
            <label for="email">Your E-mail</label>
            <input type="text" id="email" name="email" placeholder="Your E-mail adress..."> <span class="error">* <?php echo $emailErr;?></span>

            <label for="message">Your message</label>
            <textarea id="message" name="message" placeholder="Write your message here / the reason why you want to contact us " ></textarea>

            <input type="submit" name="submit" value="Submit">
        </form>
    </div>


<?php 
if(isset($_POST['submit'])){
$to = "[email protected]"; 
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} ?>
<?php
   $nameErr = $emailErr = "";
   $name = $email = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
}?>
0

4 Answers 4

1

Move the form HTML code below all of the PHP code otherwise your error variables such as $emailErr won't be displayed as they are not defined before they are used.

Sign up to request clarification or add additional context in comments.

Comments

0

Re-positioned code blocks in their proper places. Also deleted unneeded codes.

Try:

<?php 

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

if(isset($_POST['submit'])){
    $emailErr = "";
   $name = $email = $comment = "";
   $nameErr  = "";

  if (empty($_POST["fullname"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["fullname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if( trim( $emailErr ) == "" AND trim( $nameErr ) == "" ) {
        $to = "[email protected]"; 
        $from = $_POST['email'];
        $first_name = $_POST['fullname'];
        $last_name = $_POST['last_name'];
        $subject = "Form submission";
        $subject2 = "Copy of your form submission";
        $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
        $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

        $headers = "From:" . $from;
        $headers2 = "From:" . $to;
        mail($to,$subject,$message,$headers);
        mail($from,$subject2,$message2,$headers2);
        echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    }

}?>
<style>.error { color:red; } </style>
<!-- <iframe name="formDestination" class="nnn"></iframe> -->
<div class="container33">
        <form action="" class="thwid" method="post">
            <label for="fname">Full Name</label>
            <input type="text" id="fname" name="fullname" placeholder="Your full name..." value="<?php echo @$name;?>"><span class="error">* <?php echo @$nameErr;?></span>
            <label for="email">Your E-mail</label>
            <input type="text" id="email" name="email" placeholder="Your E-mail adress..."> <span class="error">* <?php echo @$emailErr;?></span>

            <label for="message">Your message</label>
            <textarea id="message" name="message" placeholder="Write your message here / the reason why you want to contact us " ></textarea>

            <input type="submit" name="submit" value="Submit">
        </form>
    </div>

Comments

0
<?php
   $nameErr = $emailErr = "";
   $name = $email = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
}?>
<div class="container33">
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post"  target="">
      <label for="fname">Full Name</label>
      <input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>">
      <span class="error">* <?php echo $nameErr;?></span>
      <br>

      <label for="email">Your E-mail</label>
      <input type="text" id="email" name="email" placeholder="Your E-mail adress..."> 
      <span class="error">* <?php echo $emailErr;?></span>  
      <br>

      <label for="message">Your message</label>
      <textarea id="message" name="message" placeholder="" ></textarea>

      <input type="submit" name="submit" value="Submit">
  </form>
</div>

Comments

0

Try this.

<?php
   $nameErr = $emailErr = "";
   $name = $email = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST["firstname"]) && $_POST["firstname"] != "") {

    $name = test_input($_POST["firstname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
      echo $nameErr;
    }

    } else {
       $nameErr = "Name is required";
       echo $nameErr;
  }

  if (isset($_POST["email"]) && $_POST["email"] != '') {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
    echo $emailErr;

  } else {
    $emailErr = "Email is required";
    echo $emailErr;
  }

  if (isset($_POST["comment"]) && $_POST["comment"] != '') {
     echo $comment;
     $comment = test_input($_POST["comment"]);
  } else {
     $comment = "";
     echo $comment;
  }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
}?>

You need to echo the value if you get any error.

Tip : Always use isset to check if the value is set or not. Also do the same in your email function.

1 Comment

Now i just get "syntax error, unexpected '$_POST' (T_VARIABLE), expecting" when i try to load taht page. its line 19 in the code taht you posted here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.