简体   繁体   中英

Cannot post my php file, using xampp

I'm trying to set up an HTML contact form on my website that will use PHP to send the information entered into the form to my email address.

Unfortunately, I keep getting an error that says Cannot POST /form.php and I can't figure out why.

I looked through similar questions that were asked here on StackExchange and the number one cause seemed to be that the PHP file wasn't in the same folder as the index.html file. However, both of my files are in the same folder; newwebsite/index.html and newwebsite/form.php .

So, why could this be happening?

Here's my index.html form:

<form method='post' action='form.php'>
                <label>Name: </label>
                <input type='text' name='vistorName' id='form-name' placeholder="Type your name.." required>
                <br>
                <label>Email: </label>
                <input type='email' name='vistorEmail' id='form-email' placeholder="Type your email.." required>
                <br>
                <label>Msg: </label>
                <textarea type='text' name='vistorMsg' id='form-msg' placeholder="Type your message here..." required></textarea>
                <br>
                <input type='submit' action="submit" value='Submit' id='submit-button' required><br>
            </form>

And here's my form.php file:

<?php
    //Pulling values that were entered into the form.
$name = $_POST['visitorName'];
$email = $_POST['visitorEmail'];
$message = $POST['visitorMsg'];

//Structure of email that I will receive with the form info
$email_from = "myemail@email.com";
$email_subject = 'New Contact Form Message';
$email_body = "You have received a new message from $name. \n".
                "Here is the message: \n $message".

//Sending to my email address and using the mail function
    $to = "zcericola@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);

//Validating the form
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;
    }

}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}





?>

//HTML confirmation that message was sent.
    <html lang='en'>
    <h1>Your email has been sent. Thank-you!</h1>



    </html>

With a few modifications, your code worked on my server.

Per Akintunde's comment, your form has typos in the field names:

<html>
<form method='post' action='form.php'>
                <label>Name: </label>
                <input type='text' name='visitorName' id='form-name' placeholder="Type your name.." required>
            <br>
            <label>Email: </label>
            <input type='email' name='visitorEmail' id='form-email' placeholder="Type your email.." required>
            <br>
            <label>Msg: </label>
            <textarea type='text' name='visitorMsg' id='form-msg' placeholder="Type your message here..." required></textarea>
            <br>
            <input type='submit' action="submit" value='Submit' id='submit-button' required><br>
        </form>

Your "form.php" checks the e-mail address after attempting to send the e-mail. Also, html comments are identified differently than php comments. I made those two changes.

<?php
//Pulling values that were entered into the form.
$name = $_POST['visitorName'];
$email = $_POST['visitorEmail'];
$message = $POST['visitorMsg'];

if(IsInjected($email))
{
echo "Bad email value!";
exit;
}


//Structure of email that I will receive with the form info
$email_from = "myemail@email.com";
$email_subject = 'New Contact Form Message';
$email_body = "You have received a new message from $name. \n".
            "Here is the message: \n $message".

//Sending to my email address and using the mail function
$to = "zcericola@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);

//Validating the form
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;
}

}



?>
    <html lang='en'>

<!-- HTML confirmation that message was sent. -->
    <h1>Your email has been sent. Thank-you!</h1>

    </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM