Endô Fujimoto
Endô Fujimoto

Reputation: 311

PHP Contact Form Blank Data & Unknown Sender

Hello I have made a contact form on my website but even if the user doesn't type in any of the details and presses the submit button it sends me a blank email with nothing in it from Unknown Sender.

Anyone know why this is happening? I have added form validation so it shouldn't be sending anything.

HTML Code:

<form action="mail.php" method="POST">
<font color="red">*</font> Name <input type="text" name="name" required>
<font color="red">*</font> Phone <input type="text" name="phone" required>
<font color="red">*</font> Email <input type="text" name="email" required>
<font color="red">*</font> Message <input type="text" name="message" placeholder="I am looking for..." required><br />
<input type="image" src="images/Landing_Pages/submit.png" border="0" alt="Submit" />
</form>

PHP Code:

<?php $name = $_POST['name'];
    $phone = $_POST['phone'];
    $preferred = $_POST['preferred'];
    $email = $_POST['email'];
    $formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
    $recipient = "[email protected]";
    $subject = "New Request Southbank";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    ?>

Image

Upvotes: 0

Views: 125

Answers (2)

Sayed
Sayed

Reputation: 1062

You have to check if post data is not empty

 if (!empty($name) && !empty($phone) && !empty($preferred) && !empty($email)) {
      mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
 }

Edit

<?php
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $preferred = $_POST['preferred'];
    $email = $_POST['email'];
    $formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
    $recipient = "[email protected]";
    $subject = "New Request Southbank";
    $mailheader = "From: $email \r\n";
    if (!empty($name) && !empty($phone) && !empty($message) && !empty($preferred) && !empty($email)) {
        mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    }
?>

Upvotes: 4

SayJeyHi
SayJeyHi

Reputation: 1841

Do something like :

$errorflag = 0 ;
if(empty($name)){$errorflag = 1 ;$error = "Input your Name plz!"; }; 
if(empty($phone)){$errorflag = 1 ;$error = "Input phone"; }
...
if(!$errorflag) {
    @mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}

check every input you need and then send

Upvotes: 1

Related Questions