1

I am building a form that the filled data need to be sent to an email. I am using HTML and PHP. But Unfortunately I get the following error when I press SUBMIT button.

405 - HTTP verb used to access this page is not allowed. The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access

The PHP code is :

<?php

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    
    $email_subject = "NEW MESSAGE VIA CONTACT FORM";
    
    $email_body = "You have received a new message from the user ".$name. ".\n".$message;
    
    $to = "info@house.gr"
    $headers = "From: ".$visitor_email;
    
    mail($to, $email_subject, $email_body, $headers);
    header("Location: index.html?mailsend");
}
?>

The HTML form is :

<form method="POST" class="contact-form" actions="contactform.php">
                                <input type="text" name="name" placeholder="Full Name">
                                <input type="text" name="mobile" placeholder="Your Mobile Phone">
                                <input type="text" name="email" placeholder="Your E-mail">
                                <textarea class="materialize-textarea" name="message" placeholder="Your Message"></textarea>
                                <input type="submit" value="Submit" class="btn">
                            </form>

UPDATE: with this edit on code: action="contactform.php" , I dont get the error but the email is not send

FINAL SOLUTION: Changed the php file to this:

    $headers = "From: ";
    $headers .= $visitor_email;
    
    mail($to, $email_subject, $email_body, $headers);
    header('Location: ../');

I found the problem via the log files in the webserver

TheGame
  • 53
  • 2
  • 9
  • 1
    Typically, a 405 error means you are sending a request to an endpoint that doesn't know how to handle that request type. Make sure you are directing to the right endpoint and that it is set up to handle POST requests. – The Head Rush Jan 11 '22 at 17:42
  • @TheHeadRush how can i check this? I followed many tutorials and they did the same thing – TheGame Jan 11 '22 at 17:44
  • No idea. I haven't worked with PHP in like 20 years. Presumably you have a controller method that maps to the `Location: index.html?mailsend` request. If so, check that it handles POST requests. If not, make a handler that does. – The Head Rush Jan 11 '22 at 17:48
  • Are you using IIS? Quick searches for that specific error message detail are showing that you might need [disable WebDAV](https://stackoverflow.com/a/9854860/231316) or possibly just allow POST to that file type. – Chris Haas Jan 11 '22 at 17:48
  • @ChrisHaas I am running through the server directly so i am not sure WebDAV is smth I can adjust – TheGame Jan 11 '22 at 18:11

1 Answers1

0

maybe you wrote actions instead of action in the form's attribute

actions="contactform.php"

should be

action="contactform.php"