I have a php script for the capture of a name and email address for a mailing list. If possible could someone please offer some adivce on how to make the email and name fields as required, so user is forced to input their name and email into form fields.
Again much appreciated for any help !!
The following is the php code being used for the form
<?php
$sendTo = "info@mail.com";
$subject = "website email enquiry";
$headers = "From: " . $_POST["firstName"] ." ". $_POST["lastname"] . "<" . $_POST["email"] .">\r\n";
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";
$headers .= "Return-path: " . $_POST["email"];
$message = $_POST["message"];
mail($sendTo, $subject, $message, $headers);
?>
The above code will validate the contents of your e-mail post variable to be a valid email address.
You have to check values of
$_POST['firstname']
,$_POST['lastname']
and$_POST['email']
For the the name, you can check it with :
empty()
You can also, use strlen() and trim() to check string size and not validate a name with only 1 character length.
For email, you can check it with :
filter validate
You don't have to do this in PHP anymore. In your form where you have your email filed/textbox, just insert required and type as email then the form won't submit unless this fill contains a valid email address.
That should do the trick. HTML5 Baby!
Here's the best way to do this in PHP.
This should do the trick.