[英]post to form to same page
基本上,我正在嘗試重新處理插件,因此我需要做的是將表單數據發布在同一頁面上的Cart.php表單上。 以下是我的設置,但發送電子郵件時$_POST
信息未返回任何內容:
Cart.php
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
</div>
</form>
<?php
//Send the email
$to = "test@gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
?>
如果您立即加載,則發送的郵件中沒有任何內容(或缺少信息),這是由於特定原因造成的。
就目前而言,您的代碼會在您加載頁面后立即向您發送一封電子郵件,因此最好將(PHP)代碼包裝在isset()
以與(命名)提交按鈕結合使用。您最初發布的問題/代碼中缺少。
另外,您有兩個未定義的變量:
$mine
和$flygt
,所以您需要定義滿足您需求的那些。
即: if(isset($_POST['submit']))
和<input type="submit" name="submit" value="Send">
旁注:最好檢查是否有空字段,但這是另一個主題。 看到我的腳注。
經過測試,可以正常工作,並接收所有信息,我已經用if(mail($to, $subject, $emailBody, $headers)){...}
替換了您當前的郵件功能
<form id='SimpleEcommCartCartForm' action="" method="post">
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
<input type="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
//Send the email
if(isset($_POST['submit'])){
$to = "test@gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
// $mine = " Mine variable"; // replace this
// $flygt = " Flygt variable"; // replace this
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
// $send = mail($to, $subject, $emailBody, $headers);
if(mail($to, $subject, $emailBody, $headers)){
echo "Mail sent.";
}
else{
echo "Sorry, something went wrong.";
}
} // brace for if(isset($_POST['submit']))
?>
如果仍然有問題:
在打開<?php
標記后,將錯誤報告添加到文件頂部,這將在生產測試中提供幫助。
error_reporting(E_ALL);
ini_set('display_errors', 1);
腳注:
使用以下(PHP)篩選器函數: FILTER_SANITIZE_FULL_SPECIAL_CHARS
$name = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
等效於設置了
ENT_QUOTES
htmlspecialchars()
調用。 可以通過設置禁用編碼引號。
要檢查是否有空字段,可以在if(isset($_POST['submit'])){
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone'])){
echo "Fill out all the fields";
exit;
}
||
意思是“OR”,這很可能被替換為OR
但是如果你想, ||
優先於OR
。
這是一個非常基本的方法。 還有其他方法可以做到這一點,但是您可以領會到它的要旨。
發送電子郵件后,您沒有告訴PHP向頁面輸出任何內容。
您可以添加echo 'Mail sent';
到PHP的末尾,因此如果腳本執行正確,那么您將了解它。
如果問題出在根本未發送的電子郵件中,則可能是服務器有問題,您應該尋找有關如何正確設置郵件的教程。 如果您使用共享主機,則有些公司不允許使用PHP mail()
函數,因此我將首先與他們聯系。
在這當中,我建議您使用PHPMailer之類的東西,它是PHP的完整電子郵件庫。 然后,您可以配置將電子郵件從支持SMTP的任何地方發送(不確定是否支持其他電子郵件,但很有可能支持)。 PHPMailer將與共享主機一起使用。
更新-您可以嘗試使用的代碼
嘗試用它代替$emailBody
:
$emailBody = <<<EOT
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<h1>Pump Inquiry</h1>
<h3>From: $name<h3>
<h3>Phone: $phone<h3>
<p>Minetuff Parts $mine</p>
<p>Flygt Parts $flygt</p>
</body>
</html>
EOT;
注意:有些電子郵件客戶端只允許使用內聯CSS,因此,如果您確實決定在<head>
<style>
標記中添加任何內容,請不要指望它能很好地工作或在某些情況下根本無法工作。
如前所述,您還需要防止跨站點腳本編寫。
這里的問題是,由於您沒有測試$_POST
變量的存在,因此郵件是在提交之前發送的,請嘗試執行以下操作:
<form id='SimpleEcommCartCartForm' action="" method="post">
<input type="hidden" name="action" value="mailing"/>
<div id="emailForm">
<p>Please fill out the form below and one of our associates will contact you with more information.</p>
<div class="col-xs-4">
Name: <input type="text" name="name" id="name">
</div>
<div class="col-xs-4">
E-mail: <input type="email" name="email" id="email">
</div>
<div class="col-xs-4">
Phone: <input type="tel" name="phone" id="phone">
</div>
<input name="submit" type="submit"/>
</div>
</form>
<?php
if(isset($_POST['mailing'])){
// Send the email
$to = "test@gmail.com";
$name = $_POST['name'] ;
$from = $_POST['email'] ;
$phone = $_POST['phone'] ;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from";
$subject = "Pump Part Inquiry";
$emailBody = "
<html>
<head>
<style>
</style>
</head>
<body>
<h1> Pump Inquiry</h1>
<h3>From:".$name."<h3>
<h3>Phone:".$phone."<h3>
<p>Minetuff Parts".$mine."</p>
<p>Flygt Parts".$flygt."</p>
</body>
</html>";
$send = mail($to, $subject, $emailBody, $headers);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.