简体   繁体   中英

Contact Form Posting

I have created a html, php and js files for my Contact Form. I upload it to my Hosting Provider just for testing as I don't have a live server. When I fill out all required fields and hit Submit, I don't get information send to email address. This is just a simple form so that customers can send me their query and details.

 $(function() { $(".form-control").on('focus', function() { $(this).parents(".form-group").addClass('focused'); }); $(".form-control").on('blur', function() { $(this).parents(".form-group").removeClass('focused'); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <div class="container"> <form action="form-handler.php" method="POST" class="form"> <div class="form-group"> <label for="name" class="form-label">Your Name</label> <input type="text" class="form-control" id="name" name="Name" placeholder="Jane Doe" tabindex="1" required> </div> <div class="form-group"> <label for="email" class="form-label">Your Email</label> <input type="text" class="form-control" id="email" name="Email" placeholder="Jane@Doe.com" tabindex="2" required> </div> <div class="form-group"> <label for="subject" class="form-label">Subject</label> <input type="text" class="form-control" id="subject" name="Subject" placeholder="Subject" tabindex="3" required> </div> <div class="form-group"> <label for="message" class="form-label">Message</label> <textarea class="form-control" name="message" id="message" cols="50" rows="5" placeholder="Write your message here..." tabindex="4"></textarea> </div> <div> <button class="btn" type="submit">Send Message</button> </div> </form> </div> </body> </html>

PHP

<?php
$message_sent = false;
if (isset($_POST['email']) && $_POST['email'] != '')
{
    if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
    {
        //submit the form
        $userName = $_POST['name'];
        $userEmail = $_POST['email'];
        $messageSubject = $_POST['subject'];
        $message = $_POST['message'];

        $to = "...@....com";
        $body = "";

        $body = "From:" . $userName . "\r\n";
        $body = "Email:" . $userEmail . "\r\n";
        $body = "Message:" . $message . "\r\n";
        mail($to, $messageSubject, $body);
        $message_sent = true;
    }
}
?>

Your code is

$body = "From:" . $userName . "\r\n";
$body = "Email:" . $userEmail . "\r\n";
$body = "Message:" . $message . "\r\n";
mail($to, $messageSubject, $body);

And from documentation I saw that a correct way to send additional headers like FROM is the folloewing:

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Another more complete way is the following:

// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers
$headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';

// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));

Finally, .. instead of code this:

mail($to, $messageSubject, $body);
$message_sent = true;

You should code this

$message_sent = mail($to, $messageSubject, $body);

Because your code assumes all works fine but mail function already return true or false if mail is sent or not.

But the most important stuff I saw is that your form ha inputs named:

  • Name
  • Email
  • Subject
  • message

and your php code check for

  • $_POST['name']; // different from Name
  • $_POST['email']; // different from Email
  • $_POST['subject']; // different rom Subject
  • $_POST['message']; // ok

So, ... if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) should always return false because $_POST['email'] email not exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM