I'm using a basic php for to mail myself from a website.
It's a single page website with the required PHP inside it, rather than a separate mail.php. I've used the built in validation and required fields, however when I visit the domain rather than domain/index.php the form code changes to make the action mail.php, no validation takes place and a blank email is sent.
I want it to have all fields required, validated then display a small success message to let the user know their message has been sent. I've attached my code below.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);
$formcontent="From: $name \n Message: $message";
$recipient = "me@domian";
$subject = "Contact Form Subbmission";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo ("Thanks for getting in touch, I'll get back to you as soon as possible.");
}
//clean it
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>#contact">
<div class="row 50%">
<div class="6u"><input type="text" name="name" placeholder="Name" required="required"/></div>
<div class="6u"><input type="email" name="email" placeholder="Email" required="required"/></div>
</div>
<div class="row 50%">
<div class="12u"><textarea name="message" placeholder="Message" rows="6" required="required"> </textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
</div>
</form>
Thanks in advance for any help you can offer
Try this:
<?php
//checks if the post variables are set and also checks that they aren't empty
if(isset($_POST['name'],$_POST['email'],$_POST['message'])&& $_POST['name'] !="" && $_POST['email'] !="" && $_POST['message'] !=""){
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);
$formcontent="From: $name \n Message: $message";
$recipient = "me@domian";
$subject = "Contact Form Subbmission";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader);
echo 'Thanks for getting in touch, I\'ll get back to you as soon as possible.';
}else{
echo 'You didn\'t fill out all the fields. Please try again';
}
//clean it
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="index.php" method="post">
<div class="row 50%">
<div class="6u"><input type="text" name="name" placeholder="Name" required="required"/></div>
<div class="6u"><input type="email" name="email" placeholder="Email" required="required"/></div>
</div>
<div class="row 50%">
<div class="12u"><textarea name="message" placeholder="Message" rows="6" required="required"></textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
</div>
</form>
In every case use better error handling then "or die" with the mail function.
eg;
<?php
if(mail($recipient, $subject, $formcontent, $mailheader)){
echo ("Thanks for getting in touch, I'll get back to you as soon as possible.");
}
?>
Your form doesn't need this action. If it is posted to the domain itself, use the following:
<form method="post" action="#contact">
I just made a separate mail.php file on the server, this project is already over schedule.
Thanks for your answers
K
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.