Using an if, else conditional: if (condition) {do something} else { do this} , we can make certain fields of our form required that they be filled out. Let's start by making a basic form with first name, last name, email and comments as our text inputs. Copy the following code into a text document then save and name it form.html:
#form { width: 500px; margin-right: auto; margin-left: auto; } -> |
if (!empty($_POST['fname'])){ $msg = "fname; $_POST[fname] "; }else{ $fname = NULL; echo "Please fill out your first name. "; } |
. The " " that you see in the string simply adds a line break to the information being sent so that when the recipient receives the email, its not one continuous line.
So, the entire PHP script to check all text inputs is:
if (!empty($_POST['fname'])){ $msg = "fname; $_POST[fname] "; }else{ $fname = NULL; echo "Please fill out your first name. "; } if (!empty($_POST['lname'])){ $msg .= "lname: $_POST[lname] "; }else{ $lname = NULL; echo "Please fill out your last name. "; } if (!empty($_POST['email'])){ $msg .= "email: $_POST[email] "; }else{ $email = NULL; echo "Please leave your email address. "; } if (!empty($_POST['comments'])){ $msg .= "comments: $_POST[comments] "; }else{ $comments = NULL; echo "You forgot to leave a comment. "; } $recipient = "yourname@yourdomain.com"; $subject = "Form Feedback"; $mailheaders = "Reply-to: $_POST[email]"; //send the mail mail($recipient, $subject, $msg, $mailheaders); ?> |
No comments:
Post a Comment