[英]Cannot POST /get.php
我看过Stack Overflow上的许多其他线程,尽管我看不到为什么我的代码未提交,或者它只会返回php代码本身。 我尝试下载XAMPP,尽管我根本不了解如何使用它。 我正在尝试通过电子邮件将表单发送到指定的电子邮件地址,但是它将给我“无法发布”或返回代码本身(在添加其他变量之前)。 任何帮助将不胜感激。
这是我的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>
再次感谢您提供的任何建议!
您的$_POST
参数看起来不错; 问题似乎只是您已忽略创建$subject
变量。 简单地创建此变量(或将其分配给$_POST
参数)应该可以解决您的问题: $subject = $_POST['subject'] ?: 'You have mail';
。
为了确保在省略$_POST
数据时正确设置变量,我还建议设置默认值,如上面的$subject
行所示,它使用了三元运算符 。 如果设置了$_POST['subject']
,则使用它。 如果不是,则使用'You have mail'
主题。
请注意, 除了 if(mail())
之外 ,您不需要调用mail()
if(mail())
,因为条件本身将触发发送。 就目前而言,您的代码将邮寄两次,因此您需要删除不在条件内的代码。
顺便说一句,您还有$to
和$email
变量都来自$_POST['email']
; 您可以在此处保存变量。
根据您提供的关于无法安装XAMPP的详细信息,并且看到的是“代码本身”,我猜测您没有能够运行PHP代码的有效Web服务器。 您将需要一个,因为仅通过在本地指向浏览器就无法执行PHP。
如果您认为安装XAMPP超出了您的能力,则可能会考虑使用共享的Web主机(Hostgator,Dreamhost,许多其他主机)为您托管代码。 另外,这也是我的建议,您可以学习如何在廉价的小型VPS上安装LAMP(Linux,Apache,MySQL,PHP)堆栈。 有上百万个HOWTO和如何在线完成的演练。 我推荐的其中之一是Digital Ocean ,他也以每小时几美分的价格租用了这种便宜的VPS(他们最便宜的每月5美元的服务器足以应付这类事情)。
您尚未声明$subject
变量,这是PHP的mail()
的必需参数,否则您将无法发送邮件,并且脚本将失败。
并且您两次调用了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";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.