HTTP ERROR 405 on submitting HTML form when using PHP mail - php

I am trying to send mail to my account using PHP but it show HTTP ERROR 405 when i submit my HTML form.
Here's my HTML form:
<form action="spledmailer.php" method="post" enctype="text/plain" class="needs-validation" novalidate>
<div class="container text-center w-50">
<input type="text" class="form-control" placeholder="Enter Your Name" name="name" required><br>
<input type="email" class="form-control" placeholder="Enter Your E-mail" id="email" name="Email" required><br>
<textarea class="form-control" rows="5" placeholder="Tell Us About Your Request" name="query" required></textarea>
</div>
<div class="container" style="margin-top: 3%;">
<div class="row">
<div class="col text-center">
<button type="submit" class="btn btn-outline-dark">Send Query</button>
</div>
</div>
</div>
</form>
And here's my PHP code:
if(isset($_POST['submit'])) {
$to = "example#gmail.com";
echo $subject = "Form Tutorial";
echo $name_field = $_POST['name'];
echo $email_field = $_POST['Email'];
echo $message = $_POST['query'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}

If you are using a web host, you might want to ask them if they block POSTing to certain files, or just the php mail function in general. This has been a problem for me in the past, so contact their support.
Also, make sure you enable error reporting or look through your error log. That can probably tell you what the problem is. See here: How can I get useful error messages in PHP?

Related

Question about contact form HTTP ERROR 405 crash

so I am trying to make a link to contact form but it seems to be crashing and I do not know why is this happening. I double checked the link and variable name, but seems like error 405 happens without stopping every time I click.
This is my html part
<h3>Contact Form</h3>
<div class="containerofcontact" style="text-align: left !important;">
<form method="POST" action="contactform.php">
<label for="name">Name</label>
<input type="text" id="Name" name="Name" placeholder="Your name..">
<label for="email">Email</label>
<input type="text" id="Email" name="Email" placeholder="Your Email">
<label for="subject">Subject</label>
<textarea id="Subject" name="Subject" placeholder="Place Detail of Concern" style="height:200px"></textarea>
<input type="submit" value="Send">
</form>
</div>
This is php part
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$text = $_POST['Subject'];
$mailTo = "blankforshare";
$headers = "From: ".$email;
$txt = "You have received an e-mail from ".$name."\n\n".$text;
mail($mailTo, $headers, $txt) or die("Error!");
header("Location: index.html");
?>
Thank you for the help.

Contact Form submission failing?

I'm currently working on my portfolio. As of right now I'm trying to get the contact form to work. So I don't really know php that well and I'm not that good at it, but I've been googling and trying different ideas for myself. As of right now none of them have worked. Below I will attach the contact form html portion as well as the php I have now. Any help would be appreciated greatly
<section id="contact-form">
<!--Contact form section-->
<form method="POST" name="emailForm" action="index.php">
<!--Beginning of form and mail to my email address-->
<div class="contact-left">
<!--Contact info on the left side-->
<h1 class="c-l-heading">Contact</h1>
<!--Major heading-->
<div class="f-name">
<!--Name portion-->
<font>Name</font>
<input type="text" name="name" placeholder="Full Name" required/>
<!--Input field with a placeholder-->
</div>
<div class="f-email">
<!--email portion-->
<font >Email</font>
<input type="email" name="name" placeholder="Example#gmail.com" required/>
<!--Input field with a placeholder-->
</div>
</div>
<div class="contact-right">
<!--Contact info on the right side-->
<div class="message">
<!--Message portion-->
<font >Message</font>
<textarea name="message" rows="5" cols="20" placeholder="Write Message..." required></textarea>
<!--Textbox created-->
</div>
<button>submit</button>
<!--Submit button-->
</div>
</form>
</section>
PHP section now:
<?php
if(!isset($_POST['submit']))
{
echo "Error! You need to submit the form!";
}
//Collection
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validation
if(empty($name) || empty($visitor_email))
{
echo "Name and email are required"
exit;
}
$email_from = 'desmondlambkin17#gmail.com';
$email_subject = "dnjls.ca Contact Submission";
$email_body = "Name: $name\n"
"Email address: $visitor_email\n"
"Message: $message";
$to = "desmondlambkin17#gmail.com";
$headers = "From: $email_from";
//Send Email
mail($to, $email_subject, $email_body, $headers);
?>
Currently when I try to submit an email it says This page isn't working, and is currently unable to handle the request

Contact Form - How to not redirect to another page after submission [duplicate]

This question already has answers here:
Submit form and stay on same page?
(5 answers)
Submit form without reloading or leaving current page (php- mysql)
(2 answers)
How to submit an HTML form without redirection
(12 answers)
Submit form without page reloading
(19 answers)
Stop form refreshing page on submit
(20 answers)
Closed 4 years ago.
I have a contact form that works fine but my issue is it goes to another page if you click submit and echos "Email Sent!"
How can I prevent such redirect? I would like it to just pop under the button, or the button disappears and will be replaced with "Email Sent!" or "Error!"?
Here's my HTML:
<div style="padding-top: 75px; padding-left: 0;" class="col-md-6 col-sm-6 col-xs-12 contact-padding hidden-xs hidden-sm" id="form_container">
<div class="row">
<div class="col-centered1 col-md-8">
<h2 style="font-family: 'yrthree_boldregular', sans-serif;" class="divider-header3">Hit us up</h2>
</div>
</div>
<form role="form" method="post" id="contact-form" action="mail.php" name="contact-form">
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<input type="text" id="name" name="name" class="form-control" placeholder="NAME" required>
</div>
</div>
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<input type="text" id="email" name="email" class="form-control" placeholder="EMAIL" required>
</div>
</div>
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<textarea style="resize: none; border: 0; color: #07002c;" class="form-control" type="text" name="message" id="message" placeholder="MESSAGE" maxlength="6000" rows="7"></textarea>
</div>
</div>
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<a class="btn btn-primary pull-left send-button1" onclick="document.getElementById('contact-form').submit();">
<img draggable="false" src="/images/send-button1.jpg" alt="" onmouseover="this.src='/images/send-button2.jpg';" onmouseout="this.src='/images/send-button1.jpg';">
</a>
</div>
</div>
</form>
</div>
Here's my PHP code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$content="From: $name \n Email: $email \n Message: $message";
$recipient = "MY#EMAIL.com";
$mailheader = "From: $email \r\n";
mail($recipient, $content, $mailheader) or die("Error!");
echo "Email sent!";
?>
Thank you! :)
You can use ajax to send without redirecting to another page. I read this answer on this. You can write your code something like this:
<script type="text/javascript">
function sendEnquiryform(){
var name=$('#name').val();
var email=$('#email').val();
var message=$('#message').val();
$.post("send_mail.php",'name='+name+'&email='+email'&message='+message,function(result,status,xhr) {
if( status.toLowerCase()=="error".toLowerCase() )
{ alert("An Error Occurred.."); }
else {
//alert(result);
$('#sucessMessage').html(result);
}
})
.fail(function(){ alert("something went wrong. Please try again") });
}
Html:
<form method="post" name="FrmEnquiry" id="FrmEnquiry" action="" onsubmit="sendEnquiryform();">
<input name="name" id="name" required="required" placeholder="Your Name">
<input name="email" id="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" id="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
<span id="sucessMessage"> </span>
send mail.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = 'shridhar.kagi#gmail.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
This worked for me. Thanks to the author for answering it.
form action should be blank like
and add php code should be on same page and files ext. should be .php

How to make a div visible after email is sent using PHP

I have the following contact form in my index.html file
<div class="alert alert-success visible" id="successmsg"><strong>Success!</strong> Your message has been sent to us.</div>
<form method="post" action="mail.php" class="contact-form">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Email">
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject">
</div>
<div class="form-group">
<textarea class="form-control" type="text" name="message" id="message" placeholder="Message"></textarea>
</div>
<div class="actions">
<button type="submit" data-text="SUBMIT" class="button button-default"><span>SUBMIT</span></button>
</div>
</form>
The id "successmsg" is hidden in the HTML file using a simple display:none; code & I need to make it visible when the email is sent using the following php
<?php$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$formcontent=" Name:\r $name \n\n Email:\r $email \n\n Subject:\r $subject \n\n Message:\r $message ";
$recipient = "towersdvd#gmail.com";
$subject = "Website Contact";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader);
<style type="text/css">#lol{display:yes;}</style>
exit;}?>
This doesn't work at all. Can someone assist me with this? Really appreciate your kind help. Thanks in advance! :)
I think you are using/putting the wrong ID
Please change <style type="text/css">#lol{display:yes;}</style>
TO
<style type="text/css">#successmsg{display:inline;}</style>
Hope this will help.
There isn't any 'yes' value for the display property. Try changing it to {display: block;} or {display: inline;} depending.
Check this link for more info: https://developer.mozilla.org/en-US/docs/Web/CSS/display

Website Contact Form Not Sending Email

So I recently uploaded my first website to an iPage server. The website runs perfectly with the exception of the Contact Form which for some reason refuses to send any email whatsoever from the form.
I use a PHP Script with the Post Method, and tried to fix my code multiple times to correct any error I might have entered, but so far to no avail. Here is the code:
HTML:
<
form action = "js/mailer.php" form method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
mail($to, $subject, $body);
// redirect to confirmation
header('Location: confirmation.html');
} else {
echo "Failure";
}
?>
Can someone help me with this? It might be an error with my hosting server, or an error with my code. Your help is very much appreciated
Additional Comments: Followed the help provided (thank you for that) I made the necessary changes to the HTML and PHP, but the form is still not functional. It does not echo failure or redirect to the confirmation page, and upon inspecting the element with Firefox, I notice that upon hitting the submit button a subdivision appears under it saying "Sending....". But no email is sent, no message is echoed or page opened.
From what i see in the code you posted, the PHP mailing script won't work as you are checking if a POST variable with the name 'submit' exists which it does not as in your form the submit button does not have a name attribute.
Try giving the submit button a name and put that name in the PHP if statement.
Submit button name is missing in your form, You need to add name into your submit button,
<input type="submit" name="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
^^^^^^^^^^^^
instead of
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
Your HTML part should be something like below,
<form action = "js/mailer.php" method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="sendemail" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
In above, I have added name="sendemail" in submit button.
And your PHP script that sends email should be like
<?php
if(isset($_POST['sendemail'])) { // <-- variable name changed
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
$headers = "From: " . $email_field; // <-- Code added
mail($to, $subject, $body, $headers); // <-- Code added
// redirect to confirmation
header('Location: confirmation.html');
exit;
} else {
echo "Failure";
}
?>

Categories