[英]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.