Need some help with my Form

Hi,
I am very new to PHP. I downloaded a PHP script for form submission which also includes validation from this link:

http://form.guide/email-form/php-form-to-email.html

I then made some small changes (adding email) and uploaded the script to my new site which is here:

http://www.sadrepaintings.com/index.html

When I click on submit button, I get this error:

[i][b]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.[/b][/i]

I have followed all the instruction but can not figure out what is wrong. I will be very grateful if someone could help me.
I can even give access to the site for closer look if you want. The script is in root of the site where html files are.

Many thanks

Hamid

Post the actual code that is being used, please.

The form script is called “form-to-email.php” and is as follows:

<?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['name']; $visitor_email = $_POST['email']; $message = $_POST['message']; //Validate first if(empty($name)||empty($visitor_email)) { echo "Name and email are mandatory!"; exit; } if(IsInjected($visitor_email)) { echo "Bad email value!"; exit; } $email_from = '[email protected]';//<== update the email address $email_subject = "New Form submission"; $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $message". $to = "[email protected]";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; //Send the email! mail($to,$email_subject,$email_body,$headers); //done. redirect to thank-you page. header('Location: thank-you.html'); // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>

Form is very simple one as you see below:


Get in Touch




Name



Email



Message






There is a Thank you page that should load after the form submission as it shows below:

Thank you! Thanks for the submission!

Many thanks in advance.

I just got a success message filling out the form

Hi,
I am hosting the site on Windows 2008 which has PHP5 enabled. Do you think that is the source of the problem?

Thanks.

I would say you need a different way to send the mail out, but I did get a successful message, not an error

Good Afternoon,
I have fixed the issue with the form to email issue that I had originally after contacting the hosting company. Now the form submits successfully, BUT, it never arrives at the destinations. I contacted the hosting company and they said you need to include an “Authentication” code in your form handler and they have given me a sample below. But I don’t know how to integrate the auth. code into the original code. Below is the Auth.code. they have sent me:

require_once “Mail.php”;
$from = “Sandra Sender [email protected]”;
$to = “Ramona Recipient [email protected]”;
$subject = “Hi!”;
$body = “Hi,\n\nHow are you?”;
$host = “mail.example.com”;
$username = “smtp_username”;
$password = “smtp_password”;
$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("
" . $mail->getMessage() . "
“);
} else {
echo(”
Message successfully sent!
");
}

And this is my original code which works as you have tested it before:


<?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['name']; $visitor_email = $_POST['email']; $message = $_POST['message']; //Validate first if(empty($name)||empty($visitor_email)) { echo "Name and email are mandatory!"; exit; } if(IsInjected($visitor_email)) { echo "Bad email value!"; exit; } $email_from = '[email protected]';//<== update the email address $email_subject = "Email From Website"; $email_body = "You have received a new message from the user $name.\n". "Here is the message:\n $message". $to = "[email protected]";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; //Send the email! mail($to,$email_subject,$email_body,$headers); //done. redirect to thank-you page. header('Location: thank-you.html'); // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>

I don’t know how to mix these two together to make it work.
Any help is very much appreciated.

Best Regards,

Hamid

Please use this following code it’s with ajax or you can simple post data to PHP and get your form work done.

JS Code

$(document).ready(function(){
var form = $(’#formID’); // replace this with your forum ID rest nothing need to be change.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
console.log(‘contact form requested’);

                // Serialize the form data.
                var formData = $(form).serialize();
                // Submit the form using AJAX.
                $.ajax({
                    type: 'POST',
                    url: $(form).attr('server'),
                    data: formData
                })
                .done(function(response) {
                    //alert responce
                    alert(response);
                    document.getElementById("contactForm").reset(); 
                })
                .fail(function(data) {
                   // Set the message text.
                    document.getElementById("contactForm").reset(); 
                    if (data.responseText !== '') {
                        alert(data.responseText);
                    } else {
                   alert('Oops! An error occured and your message could not be sent.');
                    }
                });
            });
        });    
    </script>

PHP
<?php

    // Only process POST reqeusts.
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get the form fields and remove whitespace.
        $name = strip_tags(trim($_POST["name"]));
		$name = str_replace(array("\r","\n"),array(" "," "),$name);
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $message = trim($_POST["message"]);
        $phone = htmlentities($_POST['phone']);
        

        // Check that data was sent to the mailer.
        if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
        }

        // Set the recipient email address.
        // FIXME: Update this to your desired email address.
        $recipient = ""; // update your email here
        

        // Set the email subject.
        $subject = "New contact from $name";

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Phone: $phone\n\n";
        $email_content .= "Message:\n$message\n";

        // Build the email headers.
        $email_headers = "From: $name <$email>";

        // Send the email.
        if (mail($recipient, $subject, $email_content, $email_headers)) {
            // Set a 200 (okay) response code.
            http_response_code(200);
            echo "Thank You! Your message has been sent.";
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message.";
        }

    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }

?>

If you still get any problem feel free to post.

Want to make sure I understand you @fear126. Your suggestion is to use the poorly executed mail function? I spend a lot of time explaining how that isn’t reliable and shouldn’t be used for normal process. I can’t say I agree with its use here.

Can I thank you both friends for their kind responces.
@fear126, You have given me two sets of codes, one is JavaScript and the second one is the PHP code. Where do I link them to my form which I have shown the code below.
As you see, the current link is set to the PHP file called “form-to-email.php”.

Name
Email
Message

to @astonecipher , If you believe the above code is no good, may I have your suggestion please and how to integrate it.

Thank you both very much for your time.

Regards,

Hamid

For some reason the form code simply generated the output here, while I wanted to show the code. I try again without the form tag

form action=“form-to-email.php” method=“post”>


Name



Email



Message




</form

I am more in favor of mailing services after many years of using smtp.

https://mailgun.com

To #fear126

I am still awaiting your response with regards to the codes you have provided for my form. One code is JS and one is PHP, would you please provided instruction as how I would integrate them into my form code.

Thank you in advance for taking the trouble on this issue for me.

Regards,

Hamid

Sponsor our Newsletter | Privacy Policy | Terms of Service