0
votes

I am trying to make a contact form using PHP and some issue is there. I am new to PHP so couldn't figured it out. The form works if there is no validation code applied but as I apply validation code so that some fields can be made necessary, the form doesn't works right. Moreover when I leave any required field empty then them form doesn't show any error message. Can someone please tell what the problem is.

HTML Form

<form action="mail.php" method="POST" >
Name: <input type="text" name="name"><br/><br/> 
Email: <input type="email" name="email"><br/><br/> 
Phone Number: <input type="text" name="phone_number"><br/><br/>
Website: <input type="text" name="website"><br/><br/>
Message: <textarea name="message" rows="6" cols="25"></textarea><br/><br/> 
<input type="submit" value="Submit">
</form>

Main PHP Script File

<?php
if(isset ($_POST['submit'])) {
    $errors = array();

    if(!empty ($_POST ['name'])) {
        $name = $_POST ['name'];
        } else {
            $errors[] = "You forgot to enter your Name.";
        }

    if(!empty ($_POST ['email'])) {
        $email = $_POST ['email'];
        } else {
            $errors[] = "You forgot to enter your Email.";
        }

    if(!empty ($_POST ['message'])) {
        $message = $_POST ['message'];
        } else {
            $errors[] = "You forgot to enter your Message.";
        }

$phone_number = $_POST['phone_number'];
$website = $_POST['website'];
$formcontent = "From: $name \n Email: $email \n Phone Number: $phone_number \n Website: $website \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($formcontent, $recipient, $subject, $mailheader);
    if(isset($_POST['submit'])) {
        if(!empty($errors)) {
            foreach ($errors as $msg) 
                { 
                    echo '<li>'. $msg . '</li>';
                }
        } else {
            echo "Thank You";
        }
    }
}
?>

UPDATE Thanks for your replies guys, I literally forgot to have name attribute for submit button. That helped for showing some result. But now some notices are showing for undefined variables as email, message (if I provide only name in form and hit submit button) for $formcontent and $mailheader lines.

2
Since you're using !empty you'd be better off taking that code out and making the form fields required by adding that attribute, especially if you're using HTML5. - Jay Blanchard
if(isset ($_POST['submit'])) you have no name attribute for the submit to support that, therefore nothing inside it will have any use. Having used error reporting, would have thrown an Undefined index submit warning/notice. - Funk Forty Niner
your submit button doesn't have a name='submit' so $_POST['submit'] might be empty in the first place. The mail stuff should all be in the else where you have the echo "Thank you" since you don't want to mail if there are errors. And you don't need to check if isset($_POST['submit']) twice it's redundant - Dave Goten
@JayBlanchard Refer to comment number "deux". Mornin' Ralph! - Funk Forty Niner
@JayBlanchard 'tis indeed a good morning! Found a new "bobo" in OP's code. Should be putting a smile on OP's face right about now ;-) - Funk Forty Niner

2 Answers

1
votes

There are a few things wrong with your code.

if(isset ($_POST['submit'])) you have no name attribute for the submit input to support that, therefore nothing inside that conditional statement will be executed.

Having used error reporting, would have thrown an "Undefined index submit..." warning/notice.

So you need to add one:

<input type="submit" value="Submit" name="submit">
                                    ^^^^^^^^^^^^^

Then you have your mail() parameters which are not in the right order.

mail($formcontent, $recipient, $subject, $mailheader);

which should be:

  • To:
  • Subject:
  • Message:
  • Headers

Change it to:

mail($recipient, $subject, $formcontent, $mailheader);

For more information on mail(), visit the following link on PHP.net:

Edit:

You also need to place mail() function in a different place, where you have your "Thank you". Otherwise, even if an email address is not entered in the form, the mail would still be sent out, thus showing as "unknown sender" in the From. Placing mail() in the else if no errors are found.

if(isset($_POST['submit'])) {
    if(!empty($errors)) {
        foreach ($errors as $msg) 
            { 
                echo '<li>'. $msg . '</li>';
            }
    } else {

mail($recipient, $subject, $formcontent, $mailheader);

        echo "Thank You";
    }
}

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

0
votes

You can also add Anti-spam to your form

$q1 = mt_rand(1,10);
$q2 = mt_rand(1,10);
$answer = $q1 + $q2;

<form action="mail.php" method="POST" >
Name: <input type="text" name="name"><br/><br/> 
Email: <input type="email" name="email"><br/><br/> 
Phone Number: <input type="text" name="phone_number"><br/><br/>
Website: <input type="text" name="website"><br/><br/>
Message: <textarea name="message" rows="6" cols="25"></textarea><br/><br/> 
*What is <?php echo $q1 ." + ". $q2;?>? (Anti-spam):
         <input type="number" required name="Human" ><br>
 <!--question-->
          <input name="answer" id="subject" type="hidden" value="<?php echo "$answer"; ?>">
<input type="submit" value="Submit">
</form>

in your form you can check if the answer is correct

<?php
 $answer = $_POST['answer'];
 if(isset ($_POST['submit']) && $_POST['human'] == answer) {
     your mail procesing here
 }