不能POST /get.php

时间:2018-04-29 23:58:03

标签: php html

我已经查看了Stack Overflow上的许多其他线程,虽然我看不出为什么我的代码没有提交,或者它只会返回php代码本身。我尝试下载XAMPP虽然我根本不懂如何使用它。我正在尝试通过电子邮件将表单发送到指定的电子邮件地址,但它会给我一个不能POST或它将返回代码本身(在我添加额外的变量之前)。任何帮助将不胜感激。

这是我的PHP文件

<?php 
if (isset($_POST['submit'])) {
    $to = $_POST['email'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $dayPhone = $_POST['dayPhone'];
    $homePhone = $_POST['homePhone'];
    $streetAddress = $_POST['streetAddress'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipCode = $_POST['zipCode'];
    $comments = $_POST['comments'];

    $message = "
    To: $to \n
    First Name: $firstName \n
    Last Name: $lastName \n
    Email: $email \n
    Day Phone: $dayPhone \n
    Home Phone: $homePhone \n
    Street Address: $streetAddress \n
    City: $city \n
    State: $state \n
    Zip Code: $zipCode \n
    Comments: $comments \n
    ";

    $from = "me@gmail.com";
    $headers = "FROM: " . $from;

    mail($to, $subject, $message, $headers);
    if(mail($to, $subject, $message, $headers)){
        echo "Mail Sent.";
    }
    else {
        echo "failed";
    }
}

?>

这是我的HTML文件。 如您所见,我的HTML中有很多变量。我试图至少让PHP在一个单独的窗口中显示变量,所以我知道它正在工作,然后我可以努力让电子邮件单独发送

<DOCTYPE! html>
<html>
<head>
    <link rel = "stylesheet" type = "text/css" href = "styles.css">

    <div class = "Mart">Mikes Auto Mart</div>
    <div class = "header">123 Main Street <br> Anywhere, UT 88888 <br> Phone: 801:123:4567 <br> Toll Free: 1-888-888-8888</div>
    <table style = "width:100%" class = "homeTable">
    <tr>
        <td><a href = "index.html"><u>Home</u></a></td>
        <td><a href = "Inventory.html"><u>Inventory</u></a></td>
        <td><Financing</td>
        <td>About Us</td>
        </tr>
    </table>

</head>
<body>

        <br><br>
<form action = "get.php" method= "post">

First Name: <input type = "text" name = "firstName" width = "50px"><br>
Last Name: <input type = "text" name = "lastName" width = "50px"><br>
Email Address: <input type = "text" name = "email" width = "50px"><br>
Day Phone: <input type = "text" name = "dayPhone" width = "50px"><br>
Home Phone: <input type = "text" name = "homePhone" width = "50px"><br>
Street Address: <input type = "text" name = "streetAddress" width = "50px"><br>
City: <input type = "text" name = "city" width = "50px"><br>
State: <input type = "text" name = "state" width = "50px"><br>
Zip Code: <input type = "text" name = "zipCode" width = "50px"><br>
Comments: <input type = "text" name = "comments" width = "100px"><br>
<input type = "submit"  name = "submit">
    </form>
</body>
</html>

再次感谢您提供的任何建议!

3 个答案:

答案 0 :(得分:2)

您的$_POST参数看起来不错;问题似乎只是你忽略了创建$subject变量。只需创建此变量(或将其分配给$_POST参数)即可解决您的问题:$subject = $_POST['subject'] ?: 'You have mail';

为了确保在省略$_POST数据时正确设置变量,我还建议您设置默认值,如上面的$subject行中所示,它使用{ {3}}。如果设置了$_POST['subject'],则使用它。如果不是,则使用'You have mail'主题。

请注意,您无需再向mail()调用if(mail()) ,因为条件本身会触发发送。就目前而言,您的代码将邮寄两次,因此您将要删除不在条件内的那个。

另外,您还有来自$to的{​​{1}}和$email个变量;你可以在这里保存一个变量。

答案 1 :(得分:0)

根据您提供的有关无法安装XAMPP的详细信息,以及您正在查看&#34;代码本身,&#34;我猜你没有工作的网络服务器甚至执行PHP代码。您需要一个,因为您只需将浏览器指向本地就无法执行PHP。

如果您认为安装XAMPP超出了您的技能范围,也许您可​​能会想到一个共享的虚拟主机(Hostgator,Dreamhost,许多其他人)来为您托管代码。或者,这就是我建议的,你可以学习如何在一个小而便宜的VPS上安装LAMP(Linux,Apache,MySQL,PHP)堆栈。关于如何在线完成此操作,有一百万个HOWTO和演练。我推荐的其中一个是Digital Ocean,他也以每小时几美分的价格出租这些便宜的VPS(他们最便宜的5美元/月服务器就足以搞乱这样的东西)。

答案 2 :(得分:0)

您尚未声明$subject变量,这是PHP mail()中必需的参数,否则您将无法发送邮件,您的脚本将失败。

你正在调用邮件功能两次。您的PHP脚本可以是这样的:

<?php
// Verifies if the submit button was triggered
if (isset($_POST['submit'])) {

    // Defines the message content
    $to = $_POST['email'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $dayPhone = $_POST['dayPhone'];
    $homePhone = $_POST['homePhone'];
    $streetAddress = $_POST['streetAddress'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipCode = $_POST['zipCode'];
    $comments = $_POST['comments'];

    $subject = 'Whatever';

    // Mounts the message
    $message = "
    To: $to \n
    First Name: $firstName \n
    Last Name: $lastName \n
    Email: $email \n
    Day Phone: $dayPhone \n
    Home Phone: $homePhone \n
    Street Address: $streetAddress \n
    City: $city \n
    State: $state \n
    Zip Code: $zipCode \n
    Comments: $comments \n
    ";

    $from = "me@gmail.com";
    $headers = "FROM: " . $from;

    // Checks if the email has been sent
    if(mail($to, $subject, $message, $headers)){
        echo "Mail Sent.";
    }
    else {
        echo "failed";
    }
}
?>