简体   繁体   English

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

[英]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. 我正在尝试在我的网站上设置一个HTML联系人表单,该表单将使用PHP将表单中输入的信息发送到我的电子邮件地址。

Unfortunately, I keep getting an error that says Cannot POST /form.php and I can't figure out why. 不幸的是,我不断收到一条错误消息,提示Cannot POST /form.php并且我不知道为什么。

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. 我查看了在StackExchange上问过的类似问题,第一原因似乎是PHP文件与index.html文件不在同一文件夹中。 However, both of my files are in the same folder; 但是,我的两个文件都在同一个文件夹中。 newwebsite/index.html and newwebsite/form.php . newwebsite/index.htmlnewwebsite/form.php

So, why could this be happening? 那么,为什么会这样呢?

Here's my index.html form: 这是我的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>

And here's my form.php file: 这是我的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>

With a few modifications, your code worked on my server. 进行一些修改,您的代码即可在我的服务器上运行。

Per Akintunde's comment, your form has typos in the field names: 根据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>

Your "form.php" checks the e-mail address after attempting to send the e-mail. 尝试发送电子邮件后,您的“ form.php”将检查电子邮件地址。 Also, html comments are identified differently than php comments. 另外,html注释的标识与php注释的标识不同。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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