我正在处理邮件和表单。在我的表单中,我有一个从数据库填充的下拉菜单。数据库中的表有3列:id,facility和email。我需要邮件方查看设施并将$ mailto更改为数据库中的相应电子邮件地址。我真的很新,这让我难过了两天。
从 form.php下拉:
$queryData = "SELECT * FROM facilities";
$result = mysql_query($queryData);
?>
<select id="cust_Facility" name="cust_Facility">
<?php
while($row = mysql_fetch_array($result))
{
?>
<option value=<?php echo($row['id']); ?>><?php echo($row['facility']); ?></option>
<?php
}
?>
</select>
<?php
?>
形式:
$emailSubject = 'Toner Request';
$mailto = '';
$email = 'Toner Sending Service';
$facilityField = mysql_real_escape_string($_POST['cust_Facility']);
$queryEmail = "SELECT * FROM facilities WHERE ";
$result = mysql_query($queryEmail);
if (isset($facilityField)) {
echo "Facility is set";
}
$body = <<<EOD
<br><hr><br>
Customer Information<br>
<br>
Facility: $facilityField <br>
EOD;
$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Success!</h2>
</body>
</html>
当我在表单上点击提交时,它会按预期回应$ facilityField。我不知道在那之后添加什么来根据$ facilityField中的值更改$ mailto。当我手动设置$ mailto时,我按预期收到了电子邮件。
新邮件:
<?php
include('db.inc');
$emailSubject = 'Toner Request';
$mailto = '';
$email = 'Toner Sending Service';
$facilityField = mysql_real_escape_string($_POST['cust_Facility']);
$queryEmail = "SELECT email FROM facilities WHERE facility = '".$facilityField."'";
$result = mysql_fetch_assoc($queryEmail);
echo "$result\n";
if (isset($facilityField)) {
echo "Facility is set";
}
$body = <<<EOD
<br><hr><br>
Facility: $facilityField <br>
EOD;
$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
//$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Success!</h2>
</body>
</html>
根据我对Pogrindis评论的理解,我做了上述修改。它不会回应$ result。它只是给了我回声“设施设置”和“成功!”。我还缺少什么?
最终工作代码:
$queryEmail = "SELECT email FROM facilities WHERE facility = '$facilityField'";
$result = mysql_query($queryEmail);
$result = mysql_fetch_assoc($result);
$mailto = $result['email'];
答案 0 :(得分:0)
你的身体不是一根绳子。你应该收到一个错误。
你的$ body var应该是llike:
$body = "<<<EOD
<br><hr><br>
Customer Information<br>
<br>
Facility:". $facilityField ."<br>
EOD";
然后您就可以在邮件功能中使用它了。
目前,如果没有设置设施,它仍在尝试发送电子邮件。因此会发生未定义的错误。
如果您的If语句对此有帮助,请进行清理。
if (isset($facilityField)) {
echo "Facility is set";
$body = "<<<EOD
<br><hr><br>
Customer Information<br>
<br>
Facility:". $facilityField ."<br>
EOD";
$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.
}
else
echo "Please set facility. "
?>
对于正在发生的事情更加冗长的事情也会有所帮助。
你的$ mailto;在页面顶部没有定义。
如果你在表格上有它,你应该有一个电子邮件输入(发送到这个电子邮件。)或者如果你喜欢一个联系页面,它应该设置。
if(isset($_POST['emailFormName']))
$mailto = $_POST['emailFormName'];
现在通过名为emailFormName的表单输入设置mailto。
当您的更新询问如何根据设施填充mailto时。
您应该使用
$mailto = $_POST['cust_Facility'];