如何在以HTML格式提交时在PHP HTML电子邮件中添加额外信息
我一直在尽可能多地研究PHP,只是没有找到我正在寻找的信息,或者可能我已经找到了,我只是对PHP了解不够,没有告诉大家这是我需要的答案。我正在建立的这个公司的网站是关于复印机的 在我的HTML表单中,我这样做是为了让客户可以输入他们的复印机编号,并说出机器的副本数量,所有副本都使用PHP电子邮件发送,但采用HTML格式。问题是,尽管他们希望能够添加额外的机器和我在HTML页面和Javascript中使用的副本数。添加的信息不想在电子邮件中发送,但这是我的问题开始 以下是我的html代码示例:如何在以HTML格式提交时在PHP HTML电子邮件中添加额外信息,php,html,forms,html-email,Php,Html,Forms,Html Email,我一直在尽可能多地研究PHP,只是没有找到我正在寻找的信息,或者可能我已经找到了,我只是对PHP了解不够,没有告诉大家这是我需要的答案。我正在建立的这个公司的网站是关于复印机的 在我的HTML表单中,我这样做是为了让客户可以输入他们的复印机编号,并说出机器的副本数量,所有副本都使用PHP电子邮件发送,但采用HTML格式。问题是,尽管他们希望能够添加额外的机器和我在HTML页面和Javascript中使用的副本数。添加的信息不想在电子邮件中发送,但这是我的问题开始 以下是我的html代码示例: &
<form id="formexample" action="example.php" method="POST">
<div id="add-machine">
<div class="control-group">
<div class="controls">
<label class="control-label" for="equipment">Email:</label>
<input type="email" class="form-control" name="email" id="email" />
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="control-label" for="equipment">Equip. ID#:</label>
<input type="text" class="form-control" name="equipment" id="equipment" />
</div>
</div>
<div class="control-group">
<div class="controls">
<label for="meter" class="control-label">Total No. of Copies: </label>
<input type="text" class="form-control" id="meter" />
</div>
</div>
<input type="hidden" name="extraMachines" id="extraMachines" value="" />
<button type="button" id="click">Add Machine</button>
<button type="submit" id="submit">Send</button>
</div>
</form>
电邮:
装备ID#:
副本总数:
添加机
发送
现在来看我正在使用的PHP示例:
<?
$email = $_POST['email'];
$equipment = $_POST['equipment'];
$meter = $_POST['meter'];
if(empty($equipment) || empty($meter) || empty($email) || !filter_var($email,FILTER_VALIDATE_EMAIL)){
echo "No arguments Provided!";
return false;
}
else{
// create email body and send it
$to = "example@domain.com"; // ----->>> put your email to receive mails
$email_subject = "New Copier Total Submitted";
$email_body = "
<html>
<head>
<title>New Copier Total Submitted</title>
</head>
<body>
<table cellpadding='0' cellspacing='0' border='0' id='backgroundTable' align='center' style='background-color: #f7f7f7; border-width:1px; border-style:solid; border-color:#e4e4e4; width: auto'>
<tr>
<td>
<table class='Responsive' cellpadding='15' cellspacing='0' border='0' style='font-family:Georgia, Times New Roman, Times, Serif; font-size:12px; width: 650px'>
<tr>
<td style='font-family:Georgia, Times New Roman, Times, Serif; font-size:15px;'>
<h3 style='text-align: center; text-decoration:underline'>Here are the details:</h3>
<p>Email: $email_address</p>
<p>Equip ID#: $equipment</p>
<p>Total of Copies: $meter</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: example@domain.com";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>
运行时假设您的Javascript将添加一台新机器,使用迭代计算机器1
,机器2
等
正如Steven Jones提到的,这可以很容易地在发布数组的PHP端捕捉到:
如果在HTML中使用name属性,如name=“machines[]”
,则可以提交PHP要处理的数据数组。然后在PHP中,您可以执行$\u POST['machines']
,它将为您返回整个列表数组。这样做的好处是,您不需要为“额外的机器”添加额外的字段,您只需拥有一个“机器[]”列表,即可同时获取所有值
这差不多就是总结
我看不到你的Javascript但是如果你有
<input type='text' name='machine[]' value='machine 1'>
<input type='text' name='machine[]' value='machine 2'>
您可以使用foreach
浏览这些机器阵列,例如:
foreach($_POST['machine'] as $machine){
print "this is machine ".$machine."!<BR>";
}
unset($machine);
关键是这个过程对于任何数量的机器都能完美地工作,即使是零台机器 请重新思考你的问题标题。了解你的专业水平有什么好处?简单地总结一下问题的本质。或者我对PHP的了解还不够,我还不知道这是我需要的答案
这种情况经常发生在网页设计的各个领域,很高兴你意识到这是一种可能。在你的HTML中,
缺少一个名称
属性。在PHP中,不确定检查的是什么empty($)
(忘记添加变量名了?)。同样在PHP中,看起来您并没有试图获取$\u POST['extramines']
的数据。您应该执行var\u转储($\u POST)
在提交时查看提交的所有值以开始调试表单。请注意:如果有多个字段在HTML中具有相同的名称属性,如name=“machines[]”
,则可以在表单中提交数据数组供PHP处理。然后在PHP中,您可以执行$\u POST['machines']
,它将为您返回整个列表。这样做的好处是,您不需要为“额外机器”添加额外的字段,您只需拥有一个“机器[]”列表,即可同时获取所有值。@WOUNDEDStevenJones感谢您的输入,我将查看使用数组是否解决了问题。当使用foreach时,它在php文件中的排列顺序是什么?或者这有关系吗?foreach通常会从0
循环到max-1
,因为它是一个数字数组,HTML构建数组的方式是页面上的第一个元素的值为0,后面的元素的值更高。如果您需要按特定顺序显示结果,那么有很多PHP@卡夫勒
foreach($_POST['machine'] as $machine){
print "this is machine ".$machine."!<BR>";
}
unset($machine);
this is machine machine 1!
this is machine machine 2!