1

I have been turing this php form upside down but I am not finding why the error message is not displaying and the form will always send an email even if all the field are empty. there is my code:

<form name="contactform" method="post" action="">
  <input type="text" data-placeholder="<?php echo $name; ?>"name="name">
  <input type="text" data-placeholder="<?php echo $email; ?>"name="email">
  <textarea data-placeholder="<?php echo $message; ?>" name="message" rows="10"></textarea>
  <input type="submit" name="submit" class="btn btn-footer" value="ENVOYER" id="btnContact">
  <p class="text desktop"><?php echo $emailErr; ?></p>
</form>

<?php
  if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $emailErr = "";

    if (!isset($name) || $email=="" || $message=="" ) {
      $emailErr = "All fields are mendatory";
    } else {
      $mailTo = "[email protected]";
      mail($mailTo);
      $emailErr = "Email Send";
    }
 }
 ?>
4
  • Don't use isset() to see if a variable is empty. Use $name == "" like you did with the others. Commented Oct 4, 2018 at 4:39
  • Use !empty instead it will work perfectly Commented Oct 4, 2018 at 4:44
  • 1
    @sradha But what if their name is 0? Commented Oct 4, 2018 at 5:07
  • @ FrankerZ yes it will not work if we put 0 , I edited my code . Thank you for suggestion. Commented Oct 4, 2018 at 5:13

4 Answers 4

1
if (empty($name) || empty($email) || empty($message)) 
 {
     $emailErr = "All fields are mendatory";
 }

you can use php function empty or is_null

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

Comments

0

isset() will always return true in this case, because the variable has been set (To an empty string). To check to see if a variable is empty, you should use what you did with $email and $message:

if ( $name == "" || $email == "" || $message == "" ) {

To demonstrate, you can simply run the following:

$a = ""
var_dump(!isset($a)); // false
var_dump($a == ""); // true

Comments

0

Please try this ,

I check it in my local system its working fine , error and success message is coming as per your requirement

<?php
  if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $emailErr = "";

    if (!isset($name) || $email=="" || $message=="" ) {
      $emailErr = "All fields are mendatory";
    } else {
      $mailTo = "[email protected]";
      mail($mailTo);
      $emailErr = "Email Send";
    }
 }
 ?>
 <form name="contactform" method="post" action="">
  <input type="text" data-placeholder="<?php echo $name; ?>"name="name">
  <input type="text" data-placeholder="<?php echo $email; ?>"name="email">
  <textarea data-placeholder="<?php echo $message; ?>" name="message" rows="10"></textarea>
  <input type="submit" name="submit" class="btn btn-footer" value="ENVOYER" id="btnContact">
  <p class="text desktop"><?php echo $emailErr; ?></p>
</form>

1 Comment

Thanks it worked for me as well. The <php> needs to be before the <form>
0

After Form submit, if field name is exists then isset() will return true. But if you check empty value for any field you can use empty($fieldname) or $fieldname == "" or $fieldname == NULL.

Try This:

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

  if (empty($name) || empty($email) || empty($message) ) {
    $emailErr = "All fields are mendatory";
  } else {
    $mailTo = "[email protected]";
    mail($mailTo);
    $emailErr = "Email Send";
  }
}

Comments

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.