【发布时间】:2018-04-19 10:57:25
【问题描述】:
我有一个大表格,我serialize 将所有数据发送到 PHP 页面,然后通过电子邮件发送。
我的问题是:
如何在不一一调用的情况下将所有数据发送到电子邮件中?
我只调用了一些数据,例如姓名、电子邮件,但我想在电子邮件中自动添加所有其他数据。
这是我的代码:
var data = $("#FormWorkspace").serialize();
//chiamata ajax
$.ajax({
type: "POST",
url: "form-engine.php",
data: data,
dataType: "html",
success: function()
{
alert("success" + data);
},
error: function()
{
alert("no success " + data);
}
});
PHP 是:
session_start();
$nome = urldecode($_POST['nome']);
$email = urldecode($_POST['email']);
$phone = urldecode($_POST['phone']);
$company = urldecode($_POST['company']);
$nation = urldecode($_POST['nation']);
$messaggio = urldecode($_POST['messaggio']);
$myPostVar = array();
parse_str($_POST['data'], $myPostVar);
//Send mail
$to = $tua_email;
$sbj = "Richiesta Informazioni - $sito_internet";
$msg = "
<html>
<head>
<style type='text/css'>
body{
font-family:'Lucida Grande', Arial;
color:#333;
font-size:15px;
}
</style>
</head>
<body>
<h1>Richiesta Preventivo</h1>
<br />
<h2>Dati udente</h2>
<p>Nome: $nome</p>
<p>Email: $email</p>
<p>Phone: $phone</p>
<p>Company: $company</p>
<p>Paese: $nation</p>
<h3>Messaggio</h3>
<p>$messaggio</p>
<h3>Prodotti selezionati</h3>
$myPostVar // How I can write here??
<p>Fine</p>
</body>
</html>
";
$from = $email;
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= "From: $from";
mail($to,$sbj,$msg,$headers); //Invio mail principale.
【问题讨论】:
-
$_POST["data"]长什么样子? -
Ciao Luca,理论上它不应该把所有的表单数据都插入到一个数组中吧?这样我就在电子邮件中返回了一个数组
-
$nome、$email、$phone、$company 和 $nation 的内容是什么?他们应该填写表格中的数据,而不是
$_POST["data"]... -
不,我直接从
serialize获取这些数据,但我还有其他数据想插入到电子邮件中,但我不能一一调用。我该怎么办? -
我会把这个放在答案中!
标签: javascript php html ajax forms