无法发布我的php文件,使用xampp

时间:2017-10-06 17:47:34

标签: php html forms

我试图在我的网站上设置HTML联系表单,该表单将使用PHP将输入表单的信息发送到我的电子邮件地址。

不幸的是,我一直收到Cannot POST /form.php的错误,但我无法弄清楚原因。

我查看了StackExchange上提出的类似问题,第一个原因似乎是PHP文件与index.html文件不在同一个文件夹中。但是,我的两个文件都在同一个文件夹中; newwebsite/index.htmlnewwebsite/form.php

那么,为什么会这样呢?

这是我的index.html表格:

<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>

这是我的form.php文件:

<?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>

1 个答案:

答案 0 :(得分:0)

通过一些修改,您的代码可以在我的服务器上运行。

根据Akintunde的评论,您的表单在字段名称中存在拼写错误:

<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>

你的&#34; form.php&#34;尝试发送电子邮件后检查电子邮件地址。此外,HTML注释的识别方式与php注释不同。我做了两个改变。

<?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>