Php Thank you Message in same page - php

I have to validate a form and send an email..I copied this form from w3schools and attached mailto function.But i dont know how to display "Thank you for submitting" After the form is processed. Kindly Help.. The Thank you message should display on the same page below Submit Button.
Here is my code..
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$to = "dheeraj.narayan1712#gmail.com";
$subject = "My subject";
$name = "Name: $name";
$email = "$email";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$name,$headers);
?>
</body>
</html>

It's rather simple;
if(isset($email)){
echo 'Success! Thanks for submitting';
}
And then just place it after the mail function? Notice that you can change $email to any of the POST variables that you want, and also change the echo'ed content. (if you want to place it inside of the form remember to wrap it in
<?php ?>

Related

Php form doesn't display error messages

I am trying to create a form that checks and validates name, email. But I can't see any error messages. I don't know a lot of PHP, can't say that I even know the basics.
Here is the code:
<iframe name="formDestination" class="nnn"></iframe>
<div class="container33">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post" target="formDestination">
<label for="fname">Full Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span>
<label for="email">Your E-mail</label>
<input type="text" id="email" name="email" placeholder="Your E-mail adress..."> <span class="error">* <?php echo $emailErr;?></span>
<label for="message">Your message</label>
<textarea id="message" name="message" placeholder="Write your message here / the reason why you want to contact us " ></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div>
<?php
if(isset($_POST['submit'])){
$to = "myemail#cencored.com";
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} ?>
<?php
$nameErr = $emailErr = "";
$name = $email = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}?>
Move the form HTML code below all of the PHP code otherwise your error variables such as $emailErr won't be displayed as they are not defined before they are used.
Re-positioned code blocks in their proper places. Also deleted unneeded codes.
Try:
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_POST['submit'])){
$emailErr = "";
$name = $email = $comment = "";
$nameErr = "";
if (empty($_POST["fullname"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["fullname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if( trim( $emailErr ) == "" AND trim( $nameErr ) == "" ) {
$to = "2myemail#cencored.com";
$from = $_POST['email'];
$first_name = $_POST['fullname'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
}?>
<style>.error { color:red; } </style>
<!-- <iframe name="formDestination" class="nnn"></iframe> -->
<div class="container33">
<form action="" class="thwid" method="post">
<label for="fname">Full Name</label>
<input type="text" id="fname" name="fullname" placeholder="Your full name..." value="<?php echo #$name;?>"><span class="error">* <?php echo #$nameErr;?></span>
<label for="email">Your E-mail</label>
<input type="text" id="email" name="email" placeholder="Your E-mail adress..."> <span class="error">* <?php echo #$emailErr;?></span>
<label for="message">Your message</label>
<textarea id="message" name="message" placeholder="Write your message here / the reason why you want to contact us " ></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div>
<?php
$nameErr = $emailErr = "";
$name = $email = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}?>
<div class="container33">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post" target="">
<label for="fname">Full Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br>
<label for="email">Your E-mail</label>
<input type="text" id="email" name="email" placeholder="Your E-mail adress...">
<span class="error">* <?php echo $emailErr;?></span>
<br>
<label for="message">Your message</label>
<textarea id="message" name="message" placeholder="" ></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div>
Try this.
<?php
$nameErr = $emailErr = "";
$name = $email = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["firstname"]) && $_POST["firstname"] != "") {
$name = test_input($_POST["firstname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
echo $nameErr;
}
} else {
$nameErr = "Name is required";
echo $nameErr;
}
if (isset($_POST["email"]) && $_POST["email"] != '') {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
echo $emailErr;
} else {
$emailErr = "Email is required";
echo $emailErr;
}
if (isset($_POST["comment"]) && $_POST["comment"] != '') {
echo $comment;
$comment = test_input($_POST["comment"]);
} else {
$comment = "";
echo $comment;
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}?>
You need to echo the value if you get any error.
Tip : Always use isset to check if the value is set or not. Also do the same in your email function.

PHP form , showing message after submission

I am making a PHP form and not sure how to go about having the code say "message delivered" when clicking submission and having it validate correctly. Here is my code so far. I realize that the message appears anytime you click submit. Thank you for your patience.
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $comment = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (isset($_POST["submit"])) {
$message = "Message has been delivered";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="cntr">
<h2>Contact Form</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="field">
<label for="name">Name </label>
<br>
<input type="text" name="name">
<span class="error">*<?php echo $nameErr;?></span>
</div>
<div class="field">
<label for="email">Email</label>
<br>
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
</div>
<div class="field">
<label for="message">Message</label>
<br>
<textarea name="comment" rows="8" cols="40"></textarea>
</div>
<button type="submit" class="email" name="submit" value="Submit">Submit</button>
<?php echo $message; ?><br/>
</form>
</div>
Use
if (isset($_POST["submit"]) && empty($nameErr) && empty($genderErr) && empty($emailErr) && empty($websiteErr)) {
$message = "Message has been delivered";
}
instead of
if (isset($_POST["submit"])) {
$message = "Message has been delivered";
}
Because isset($_POST["submit"]) is true when ever you do a submit , so you need to check the error variables are empty also . if the error variables are empty that means your code validated and passed .

How to validate an input form using php

I have created a form as follows but now I need to validate user input using PHP. As a security measure you should not only rely on javascript/ HTML 5 form
validation to validate your form submissions. You should always employ server
side validation to verify any data that is being submitted
Write PHP code that will do the following:
1. Validate that firstName, lastName and email are required
2. Validate that age if entered is a number
3. validate the email and website entries to ensure they are valid
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<?php
$firstName="";
$lastName="";
$email="";
$age="";
$website="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstName"])) {
$firstName = "First name is required";
}
else {
$firstName = test_input($_POST["firstName"]);
}
if (empty($_POST["lastName"])) {
$lastName = "Last name is required";
}
else {
$lastName = test_input($_POST["lastName"]);
}
if (empty($_POST["email"])) {
$email = "Email is required";
}
else {
$email = test_input($_POST["email"]);
}
if (is_numeric ($_POST["age"])) {}
else { $age ="Age must be numeric";
}
}
echo $firstName;
echo $lastName;
echo $email;
echo $age;
?>
<form action="." method="POST">
<input type="text" name="firstName" placeholder="*First Name" /><br>
<input type="text" name="lastName" placeholder="*Last Name" /><br>
<input type="text" name="email" placeholder="*Email" /><br>
<input type="text" name="age" placeholder="Age" /><br>
<input type="text" "name="website" placeholder="Website" /><br>
<input type="submit" name="submit" value="Submit" />
</form>
<body>
</body>
</html>
So that it looks like this:
try this
Hint store them in array then display them
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstName"])) {
$error['firstName'] = "First name is required";
}
else {
$firstName = test_input($_POST["firstName"]);
}
if (empty($_POST["lastName"])) {
$error['lastName'] = "Last name is required";
}
else {
$lastName = test_input($_POST["lastName"]);
}
if (empty($_POST["email"])) {
$error['email'] = "Email is required";
}
else {
$email = test_input($_POST["email"]);
}
if (is_numeric ($_POST["age"])) {
}
else { $error['age'] ="Age must be numeric";
}
}
?>
<html>
<body>
<form action="" method="POST">
<?php echo $error['firstName'] = "First name is required".'<br/>';?>
<input type="text" name="firstName" placeholder="*First Name" /><br>
<?php echo $error['lastName'] = "First name is required".'<br/>';?>
<input type="text" name="lastName" placeholder="*Last Name" /><br>
<?php echo $error['email'].'<br/>'?>
<input type="text" name="email" placeholder="*Email" /><br>
<?php echo $error['age'].'<br/>'?>
<input type="text" name="age" placeholder="Age" /><br>
<input type="text" "name="website" placeholder="Website" /><br>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Here, this is a form that I happen to have in my scripts library, and you can modify it to suit your needs.
Strangely enough, it has a function called test_input() and will do what you wanted to achieve.
Sidenote: Be sure to change this to your own $myemail = "email#example.com";
<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
font-family: bookman old style;
font-size:20px;
text-align: center;
font-weight: normal;
}
h5
{
font-family: bookman old style;
font-size:15px;
text-align: center;
font-weight: normal;
}
</style>
<?php
$nameErr = $emailErr = $websiteErr = $commentErr = $categoryErr = "";
$name = $email = $comment = $website = $category = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
$Err = 1;
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$Err = 1;
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$Err = 1;
// die();
}
}
if (empty($_POST["website"])) {
$websiteErr = "URL is required";
$Err = 1;
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
// $comment = "";
$commentErr = "Comment is required";
$Err = 1;
} else {
$comment = test_input($_POST["comment"]);
}
// if (empty($_POST["category"])) {
if ($_POST["category"] == "" ) {
$categoryErr = "Category is required";
$Err = 1;
} else {
$category = test_input($_POST["category"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
URL: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error">* <?php echo $websiteErr;?></span>
<br><br>
Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><span class="error">* <br><?php echo $commentErr;?></span>
<br><br>
Category Of Site: <select size="1" name="category">
<option value="<?php echo $category;?>"> -- Please select -- </option>
<option>Arts</option>
<option>Business</option>
<option>Computers</option>
<option>Games</option>
<option>Health</option>
<option>Home</option>
<option>Kids and Teens</option>
<option>News</option>
<option>Recreation</option>
<option>Reference</option>
<option>Science</option>
<option>Shopping</option>
<option>Society</option>
<option>Sports</option>
<option>World</option>
</select><span class="error">* <?php echo $categoryErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</h5>
<?php
if(isset($_POST['submit'])){
if ($Err != 1){
$myemail = "email#example.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
$headers = "From: ". $name . " <" . $email . ">\r\n";
mail($myemail, $subject, $message, $headers);
// header('Location: submit_thanks.php');
echo "OK";
}
}
?>
I think you can try using PHP_SELF. A simple example:
HTML
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='POST'>
<input type='submit' name='submit'/>
</form>
PHP
<?php
if (isset($_POST['submit'])) {
echo "your code here";
}
?>

PHP sending two emails

I have this code I put together from other scripts and stuff I found on the internet,
Somehow it is sending me two emails, one email when I just load the page, the second of course when I submit.
Also, the header() is not sending me to the page I want it to...it just stays on the same form page, if anyone can help me find out what is going on, it would be much appreciated, I think it does have something to do with the post to self, but I can not for the love of me figure it out!
Thank you
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
font-family: bookman old style;
font-size:20px;
text-align: center;
font-weight: normal;
}
h5
{
font-family: bookman old style;
font-size:15px;
text-align: center;
font-weight: normal;
}
</style>
<?php
$nameErr = $emailErr = $websiteErr = $categoryErr = "";
$name = $email = $comment = $website = $category = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$websiteErr = "URL is required";
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["category"])) {
$categoryErr = "Category is required";
} else {
$category = test_input($_POST["category"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php include'header.php'?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
URL: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error">* <?php echo $websiteErr;?></span>
<br><br>
Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Category Of Site: <select size="1" name="category">
<option value="<?php echo $category;?>"> -- Please select -- </option>
<option>Arts</option>
<option>Business</option>
<option>Computers</option>
<option>Games</option>
<option>Health</option>
<option>Home</option>
<option>Kids and Teens</option>
<option>News</option>
<option>Recreation</option>
<option>Reference</option>
<option>Science</option>
<option>Shopping</option>
<option>Society</option>
<option>Sports</option>
<option>World</option>
</select><span class="error">* <?php echo $categoryErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</h5><?php include'footer.php'?>
<?php
$myemail = "links#loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');
?>
It's sending you two emails because you need to set your entire code inside a conditional statement.
Use isset() in conjunction with your already named submit button, which will only send mail once the submit button has been clicked and not on page load.
<input type="submit" name="submit" value="Submit">
Modify to:
<?php
if(isset($_POST['submit'])){
$myemail = "links#loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');
exit;
}
In regards to the header not redirecting is because you are outputting before header, which if error reporting had been set/on, would throw a Headers already sent... warning.
Adding ob_start(); at the top of your page and set inside <?php ?> tags sometimes help, and placed above <!DOCTYPE html...
I.e.:
<?php ob_start(); ?>
<!DOCTYPE html ...
You would be better off using an form action to another page instead of on the same page, and putting your mail codes in that file.
Another option would be to use a meta refresh method instead, if you wish to use your present code and not use a second page as the mail handler.
For example and in place of header():
$url = "submitthanks.php";
print "<meta HTTP-EQUIV=Refresh CONTENT=\"0; URL=$url\">";
Edit: - rewrite #2
Be sure to change this line $myemail = "email#example.com"; to be your Email address.
Plus, there was a mail() header missing which would most likely send mail to Spam,
and added a from Name so it's more personalized.
<?php
ob_start(); // prevents headers already sent warning
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
font-family: bookman old style;
font-size:20px;
text-align: center;
font-weight: normal;
}
h5
{
font-family: bookman old style;
font-size:15px;
text-align: center;
font-weight: normal;
}
</style>
<?php
$nameErr = $emailErr = $websiteErr = $commentErr = $categoryErr = "";
$name = $email = $comment = $website = $category = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
$Error = 1;
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$Error = 1;
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$Error = 1;
}
}
if (empty($_POST["website"])) {
$websiteErr = "URL is required";
$Error = 1;
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Comment is required";
$Error = 1;
} else {
$comment = test_input($_POST["comment"]);
}
if ($_POST["category"] == "" ) {
$categoryErr = "Category is required";
$Error = 1;
} else {
$category = test_input($_POST["category"]);
}
} // brace for if ($_SERVER["REQUEST_METHOD"] == "POST")
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php include 'header.php'; ?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
URL: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error">* <?php echo $websiteErr;?></span>
<br><br>
Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><span class="error">* <br><?php echo $commentErr;?></span>
<br><br>
Category Of Site: <select size="1" name="category">
<option value="<?php echo $category;?>"> -- Please select -- </option>
<option>Arts</option>
<option>Business</option>
<option>Computers</option>
<option>Games</option>
<option>Health</option>
<option>Home</option>
<option>Kids and Teens</option>
<option>News</option>
<option>Recreation</option>
<option>Reference</option>
<option>Science</option>
<option>Shopping</option>
<option>Society</option>
<option>Sports</option>
<option>World</option>
</select><span class="error">* <?php echo $categoryErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</h5><?php include 'footer.php'; ?>
<?php
if(isset($_POST['submit'])){
if ($Error != 1){
$myemail = "email#example.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
$headers = "From: ". $name . " <" . $email . ">\r\n";
mail($myemail, $subject, $message, $headers);
header('Location: submitthanks.php');
} // brace for if ($Error != 1)
} // brace for if(isset($_POST['submit']))
?>
you need to put this part of your code in the post section
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$myemail = "links#loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');

How do I get the data to be sent to me in an email instead of populating on the same page?

I am new to php and I have post forms down but not I want some of my imput fields to be required.
I want this form to force the user to fill out the required fields but then be directed to my process.php page which will send me an email with the data the form collected. Right now the data is being posted at the bottom of the page. Please help me direct the data to an email.
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name = test_input($_POST["name"]);}
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["website"]))
{$website = "";}
else
{$website = test_input($_POST["website"]);}
if (empty($_POST["comment"]))
{$comment = "";}
else
{$comment = test_input($_POST["comment"]);}
if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
I have the code to collect that data and email it. i just don't know how to get the form to validate and then direct to the process.php page.
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comment = $_POST['comment'];
$gender = $_POST['gender'];
$to = 'str#xxxxxxxx.com';
$subject = 'Executive Plaza Contact Form Response';
$message.= "Name: $name \n";
$message.= "Email: $email \n";
$message.= "email: $email \n";
$message.= "comment: $comment \n";
$message.= "gender: $gender \n";
$headers = "From: Eleven55\r\n";
mail($to, $subject, $message, $headers);
header('Location: thank-you.php');
Read up on the PHP mail() function here. It is basic and should get you started.
$to='you#domain.com';
$subject='Form data';
$body = "{$name} says {$comment}. \n Contact {$name} at {$email} or {$website}. \n {$name} is a {$gender}";
mail($to, $subject, $body);
http://php.net/manual/en/function.mail.php
$to='youremail#yourdomain.com';
$subject='Form data';
$body = "Name :$name \n ...";
mail($to, $subject, $body);
Read the mail function documentation for more info
My bad. Do this
document.forms.onsubmit = function(){ //onsubmit event
//validate form data using getElementById().val and so on
if(formdataisnotvalid){
alert('descriptive error message');
return false; // this will prevent the form from submitting
}

Categories