简体   繁体   English

无法发布/get.php

[英]Cannot POST /get.php

I have looked at many of the other threads on Stack Overflow although I cannot see why my code is not submitting or it will just return the php code itself. 我看过Stack Overflow上的许多其他线程,尽管我看不到为什么我的代码未提交,或者它只会返回php代码本身。 I tried download XAMPP although I do not understand how to use it at all. 我尝试下载XAMPP,尽管我根本不了解如何使用它。 I am trying to email the form to the specified email address, however it will either give me a Cannot POST or it will return the code itself(before I added the extra variables). 我正在尝试通过电子邮件将表单发送到指定的电子邮件地址,但是它将给我“无法发布”或返回代码本身(在添加其他变量之前)。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is my PHP file 这是我的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";
    }
}

?>

And here is my HTML file. 这是我的HTML文件。 As you can see, I have many variables in my HTML. 如您所见,HTML中有很多变量。 I am trying to at least get the PHP to show the variables in a separate window so I know it's working, then I can work on getting the email to send separately 我试图至少让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>

Thanks again for any advice you have to offer! 再次感谢您提供的任何建议!

Your $_POST parameters appear good; 您的$_POST参数看起来不错; the problem simply seems to be that you have neglected to create the $subject variable. 问题似乎只是您已忽略创建$subject变量。 Simply creating this variable (or assigning it to a $_POST parameter) should resolve your problem: $subject = $_POST['subject'] ?: 'You have mail'; 简单地创建此变量(或将其分配给$_POST参数)应该可以解决您的问题: $subject = $_POST['subject'] ?: 'You have mail'; .

To ensure that your variables are correctly set when your $_POST data is omitted I'd also recommend setting default values, as can be seen in the $subject line above, which makes use of the ternary operator . 为了确保在省略$_POST数据时正确设置变量,我还建议设置默认值,如上面的$subject行所示,它使用了三元运算符 If the $_POST['subject'] is set, it is used. 如果设置了$_POST['subject'] ,则使用它。 If not, the 'You have mail' subject is used instead. 如果不是,则使用'You have mail'主题。

Note that you don't need to call mail() in addition to if(mail()) , as the conditional itself will trigger the send. 请注意, 除了 if(mail()) 之外 ,您不需要调用mail() if(mail()) ,因为条件本身将触发发送。 As it stands, your code will mail out twice, so you'll want to remove the one that's not inside of the conditional. 就目前而言,您的代码将邮寄两次,因此您需要删除不在条件内的代码。

As an aside, you also have the $to and $email variables both coming from $_POST['email'] ; 顺便说一句,您还有$to$email变量都来自$_POST['email'] you can save on a variable here. 您可以在此处保存变量。

Based on the details you provided about not being able to install XAMPP, and that you are seeing the "code itself," I'm guessing you don't have a working webserver that is even executing the PHP code. 根据您提供的关于无法安装XAMPP的详细信息,并且看到的是“代码本身”,我猜测您没有能够运行PHP代码的有效Web服务器。 You'll need one, as you wont be able to execute PHP simply by pointing your browser locally. 您将需要一个,因为仅通过在本地指向浏览器就无法执行PHP。

If you think it is beyond your skills to install XAMPP, perhaps you might think about a shared webhost (Hostgator, Dreamhost, many, many others) to host your code for you. 如果您认为安装XAMPP超出了您的能力,则可能会考虑使用共享的Web主机(Hostgator,Dreamhost,许多其他主机)为您托管代码。 Alternatively, and this is what I would recommend, you can learn how to install a LAMP (Linux, Apache, MySQL, PHP) stack on a small, cheap VPS. 另外,这也是我的建议,您可以学习如何在廉价的小型VPS上安装LAMP(Linux,Apache,MySQL,PHP)堆栈。 There are a million HOWTOs and walkthroughs on how to do this online. 有上百万个HOWTO和如何在线完成的演练。 One of the ones I recommend is at Digital Ocean , who also rent out such cheap VPSes for a few cents an hour (their cheapest $5/month server is plenty for messing around with stuff like this). 我推荐的其中之一是Digital Ocean ,他也以每小时几美分的价格租用了这种便宜的VPS(他们最便宜的每月5美元的服务器足以应付这类事情)。

You haven't declared the $subject variable, this is a required param in PHP's mail() , otherwise you won't be able to send the mail and your script will fail. 您尚未声明$subject变量,这是PHP的mail()的必需参数,否则您将无法发送邮件,并且脚本将失败。

And you're calling the mail function twice times. 并且您两次调用了mail函数。 Your PHP script could be as this: 您的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";
    }
}
?>

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

相关问题 HTML5音频元素“src = get.php无法跳过位置 - HTML5 Audio Element " src = get.php cannot skip position 当我们在magento中将媒体存储用作媒体存储时,get.php不在URL中 - get.php not comes in URL when we used media storage as Media Storage in magento 如何使.htaccess将所有请求从URL重定向到get.php?u =但不包含现有文件 - How to make a .htaccess to redirect all request from the url to get.php?u= but exclusing exsisting files get.php配置以读取和删除file.txt中的行 - get.php config to read and delete line from a file.txt 我想在按下Submit按钮后在index.html上的iframe中加载get.php文件 - I want to load a get.php file in iframe on index.html once the Submit button is pressed 一种使用开关的方法,以检查“ url / file?get.php?”中是否存在“ url / file” [request uri] - A way to use switch, to check if “url/file” is present in "url/file?get.php? [request uri] jQuery post无法在php中获得$ _POST - jQuery post cannot get $_POST in php 无法获得选择选项以通过电子邮件发送 php - Cannot get select option to POST to email php 无法从 php 中的帖子获取 textarea 值 - cannot get textarea values from post in php PHP 无法从表单获取发布数据 - PHP Cannot get post data from form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM