I am trying to enable a simple contact form, however, every time on the button submit, I get redirected to /form.php with the error 405 Method Not Allowed in the console.
Here is the form in index.html
<div class="container text-center pt-5 pb-5" id="contact">
<h1 class="word">Contact Us</h1>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<form action="form.php" method="POST">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control text-white" id="name" name="name" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control text-white" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control text-white" id="message" name="message" rows="3" placeholder="Enter your message"></textarea>
</div>
<button type="submit" class="btn btn-primary mt-3 btn-lg mt-4">Send Message</button>
</form>
</div>
</div>
</div>
</div>
And here is my form.php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Set the recipient email address
$to = "hello#doctrina.ai";
// Set the email subject
$subject = "Message from Contact Form";
// Build the email content
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers
$email_headers = "From: $name <$email>";
// Send the email
mail($to, $subject, $email_content, $email_headers);
// Redirect to the thank you page
header('Location: thanks.html');
}
?>
I have thanks.html page as well. I am using Vercel for deployment. Thank you in advance for your help!
Related
I'm using an html/php form template that I found. Every time I try to submit a completed form, I get a 405 error.
I want the submit button to send the contents of the form to my email address.
Heres the html:
<div class="col-md-6">
<form action="../assets/php/actionpage.php" name="emailform" method="POST">
<div class="row mb--20">
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="Name" />
</div>
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="E-mail" />
</div>
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="Phone" />
</div>
<div class="col-sm-6 col-md-12 col-lg-6 form-group">
<input type="text" class="form-control" placeholder="Website" />
</div>
<div class="col-12 form-group">
<textarea cols="30" rows="5" class="form-control" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="col-lg-6 offset-lg-6 form-group">
<button class="btn btn-block btn--border-primary"><input type="submit" value="submit"></button>
</div>
</div>
</form>
</div>
This doesn't look out of the ordinary to me. So I think the problem is with the php.
actionpage.php:
<?php
if ( !empty($_POST) ) {}
if(!isset($_POST['submit']))
{
//This page shouldn't be accesed directly
echo "ERROR: you need to submit the form!";
exit;
}
// Collect
$name = $_POST['Name'];
$email = $_POST['E-mail'];
$phone = $_POST['Phone'];
$website = $_POST['Website'];
$message = $_POST['Message'];
// Validate
if(empty($name)||empty($email)||empty($message))
{
echo "Name, E-mail, and Message are mandatory!";
exit;
}
$email_from = $email;
$email_subject = "New Form Submission";
$email_body = "You have a new email from: $name\n their email: $email\n phone number: $phone\n and their website: $website\n Here is their message: $message";
$to = "######.##" // my email
$headers = "From $email_from \r\n";
// Send
mail($to, $email_subject, $email_body, $headers);
// redirect
header('Location: index.html');
?>
I am very new to php, so I decided to use a template.
I'm testing this using LiveServer in VScode. Is that having an impact?
I´m tring to add a success message to my form after you click submit, but i dont know how, i have tried different code from different websites, but they dont work with my code. What line do i need to add to this code please?
<form action="contact.php" method="post" id="contact">
<h1>Contact-me!</h1>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="name#example.com">
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea name="message" class="form-control" id="message" rows="3"></textarea>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" value="SEND MESSAGE">Submit</button>
<div>
</div>
</div>
</form>
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "New Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>
I have a created a html form and a php file (mail.php) and i have added this in my form <form action="mail.php" method="POST">
but When I try to submit the form, instead of emailing me and providing the thank you message, it downloads the php page. When I run the .html page in my browser, it shows up fine but when I click send it downloads mail.php, instead of working.
Any suggestions?
ok this is the code
mail.php:
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$formcontent=" From: $name \n telephone: $telephone \n comments: $comments";
$recipient = "lllkk#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" ;
test.html:
<form action="mail.php" method="POST">
<div class="row">
<div class="form-group col-md-8 col-md-offset-1">
<input type="text" name="name" placeholder="Name" class="form-control">
</div>
<div class="form-group col-md-8 col-md-offset-1">
<input type="email" name="email" placeholder="Email" class="form-control">
</div>
<div class="form-group col-md-8 col-md-offset-1">
<input type="tel" name="telephone" placeholder="telephone" class="form-control">
</div>
<div class="clearfix"></div>
<div class="form-group col-md-8 col-md-offset-1">
<textarea class="form-control" name="comments" placeholder="Comments" rows="6"></textarea>
</div>
</div>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to create a html and php contact form but it doesn't seem to be working.
I'm sure it's a simple answer but I can't figure it out.
Can anyone tell me were i'm going wrong?
It doesn't redirect to the mail.php page.
<div class="col-lg-8">
<form action="mail.php" class="form-horizontal" id="contactForm" name="contactForm" method="post" >
<div class="form-group" >
<label for="first-name" class="col-lg-2">First name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="first-name" name="first-name" placeholder="What your mam calls you ">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="last-name" class="col-lg-2">Last name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="last-name" name="last-name" placeholder="What your army buddies call you">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="email" class="col-lg-2">Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="email" name="email" placeholder="Enter your email address, we won't send you junk mail">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="website" class="col-lg-2">Your current website</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="website" name="website" placeholder="If you have one.">
</div>
</div><!--end form group-->
<div class="form-group">
<label for="message" class="col-lg-2">Any Message</label>
<div class="col-lg-10">
<textarea name="message" id="message" name="message" class="form-control"
cols="20" rows="10" placeholder="Maybe tells us a bit about your business. I'll start, mine is web development. ;)"></textarea>
</div>
</div><!--end form group-->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div><!--end of row-->
</section>
</div>
mail.php
<html>
<body>
<?php
if(isset($_POST['submit'])){
$to = "info#wonderfulwebsites.ie"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$f_name = $_POST['first_name'];
$s_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $f_name . " " . $s_name . " wrote the following:" . "\n\n" . $_POST['message'] "\n" $website = $_POST['website'];
$message2 = "Here is a copy of your message " . $f_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $f_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
</body>
</html>
Change this:
<button type="submit" class="btn btn-primary">Submit</button>
to this:
<input type="submit" class="btn btn-primary" value="Submit" />
Ok so I have the modal toggling, that is easy with bootstrap. But now, whenever I click submit the form gets redirected to php/contact-process.php. It looks like the php is processed but then just stays at a white screen, and not being redirected and no email is received to my main email(i put example#gmail.com for stackexchange question). Please help me.
<!--Modals for Contact-->
<div class="modal fade" id="contact" role = "dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal contact" name="contact" action="php/contact-process.php">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4>Contact Us</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" name="name" class="form-control" id="contact-name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label for="contact-email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" name="email" class="form-control" id="contact-email" placeholder="you#example.com">
</div>
</div>
<div class="form-group">
<label for="contact-message" class="col-lg-2 control-label">Message:</label>
<div class="col-lg-10">
<textarea name="usertext" name="message" id="" rows="8" class="form-control" style="resize:none;"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
Close
<button class="btn btn-primary" type="submit" value="Send!">Send</button>
</div>
</form>
</div>
</div>
</div>
CONTACT-PROCESS.PHP:
<?php
//add the recipient's address here
$myemail = 'example#gmail.com';
//grab named inputs from html then post to #thanks
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
//generate email and send!
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
//redirect
header("Location: index.php");
exit();
}
?>