Skip to content
Advertisement

On form submission I get an error ‘Method not allowed’ – Javascript [closed]

I have below html and javascript but on form submission I get an error Method not allowed though I would like to stay on the same index.html and show the success message.

I have debugged the javascript and it bypasses the if condition even I put valid info.

index.html

 <form method="post" name="FrmEnquiry" id="FrmEnquiry" onsubmit=" return 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>

javascript.js

    var name = $('#name').val();
    var email = $('#email').val();
    var message = $('#message').val();
    $.post('mail.php', '&name=' + name + '&email=' + email + message, function(
        result,
        status,
        xhr
    ) {
        if (status.toLowerCase() == 'error'.toLowerCase()) {
            alert('An Error Occurred..');
        } else {
            alert(result);
            $('#sucessMessage').html(result);
        }
    }).fail(function(xhr, status, error) {
        console.log(error); // this shows 'method not allowed' and then i get below alert

        alert('something went wrong. Please try again');
    });
    return false;
}

Advertisement

Answer

You need to add action tag to your form. Like this:

<form method="post" name="FrmEnquiry" id="FrmEnquiry" onsubmit=" return sendEnquiryform();" action="">

You can add the value where you want to send the post to.

Also refer to: https://www.w3schools.com/tags/att_form_action.asp

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement