0

抱歉,如果这似乎是一个愚蠢的问题,但我真的被困住了。

我正在使用带有 php 的表单来允许用户从我的网站发送邮件,并且一切正常,除非您想在表单中添加多个“ item ”、“ amount ”和“ boxeach ”。

我使用的脚本确实复制了这些项目,但使它们保持相同的名称,这意味着我的 php 只会读取并在电子邮件中发送其中一个。有什么办法可以改变这三个元素都放在电子邮件中,一个出现在另一个下面?

这是我用来复制的脚本(与按钮一起使用):

<script>$(function()  {
$('#copy').click(function(){
    var content = $('#content').html();
    var newdiv = $("<div>");
    newdiv.html(content);
    $('#content').after(newdiv);
  });
  });</script>

这是我的完整html代码:

<head>
<title>WHOLESALE ORDER</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>$(function()  {
$('#copy').click(function(){
    var content = $('#content').html();
    var newdiv = $("<div>");
    newdiv.html(content);
    $('#content').after(newdiv);
  });
  });</script>

label,a 
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px; 
}

</style>    
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>

<body>
<h2>WHOLESALE ORDER <strong><script type="text/javascript">
var date = new Date();
var month = new Array(7);
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";
var year = date.getYear();
if (year < 2000) { year+=1900; }
document.write(date.getDate() + "/" + month[date.getMonth()] + "/" + year);
</script></strong></h2>
<form method="POST" name="contactform" action="contact-form-handler.php"> 

<p>
<table><tr><td>
<label for='name'>Name:</label> <br>
<input type="text" name="name">
</td>
<td>
<label for='email'>Email:</label> <br>
<input type="text" name="email">
</td></tr></table>
</p>
<p>
<table><tr><td>
<label for='address'>Delivery Address:</label> <br>
<input type="text" name="address">
</td>
<td>
<label for='telephone'>Telephone:</label> <br>
<input type="tel" name="telephone">
</td></tr></table>
</p>
<p>
<div id="content"><table><tr><td><label for='amount'><input type="text" name="amount"></label>
<label for='items'><select name="items">
                    <option value="APPLE BAG">APPLE BAG</option>
                    <option value="APPLE BRAMLEY">APPLE BRAMLEY</option></select></label>
<label for='boxeach'><select name="boxeach">
                    <option value="BOX">BOX</option>
                    <option value="EACH">EACH</option>
                    </select></label>
</td></tr></table></div>
</p>
<p>
<input type="button" id="copy" value="Add another item +"/>
</p>
<input type="submit" value="Submit"><br>
</form>

<script language="JavaScript">
var frmvalidator  = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("email","req","Please provide your email"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>

这是我的完整 php 代码:

<?php 
$errors = '';
$myemail = 'someone@gmail.com';//
if(empty($_POST['name'])  || 
empty($_POST['email']) || 
empty($_POST['items']) || 
empty($_POST['boxeach']) || 
empty($_POST['amount']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$amount = $_POST['amount']; 
$items = $_POST['items']; 
$boxeach = $_POST['boxeach']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "WHOLESALE ORDER $name";
$email_body = "You have received a new message. ".
" Here are the details:\n $name \n $email_address \n \n \n $amount $items $boxeach"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} 
?>
4

2 回答 2

0

您遇到的问题是您没有制作一个元素数组来存储多个值。您的脚本只是重新创建一个 div。因此,简单的解决方案是将“[]”添加到您的数量、项目和 boxeach 元素中。IE

&lt;input type="text" name="amount[]" /&gt;
&lt;select name="items[]"&gt;...&lt;/select&gt;
&lt;select name="boxeach[]"&gt;...&lt;/select&gt;

比在您的 php 代码中更改这些行

$amount = $_POST['amount']; 
$items = $_POST['items']; 
$boxeach = $_POST['boxeach'];    

$amount = implode(',',$_POST['amount']);
$items = implode(',',$_POST['items']);
$boxeach = implode(',',$_POST['boxeach']);    

或以您喜欢的任何方式操作发布的数组。

希望我的对你有帮助。

于 2015-08-19T11:37:13.750 回答
0

当您单击 $("#copy) 时,您会创建一个包含信息的 div,该 div 会重写之前的。

您可以做的是在#content div之外进行隐藏输入,因为您正在重写它的html。

<input type="hidden" name="name_string">

在创建 div 时单击也会将信息附加到隐藏的输入中:

$('#copy').click(function(){
    var hidden =  $("input[name='name_string']);
    hidden.val(hidden.val() +","+ $("input[name='name']).val());
    var content = $('#content').html();
    var newdiv = $("<div>");
    newdiv.html(content);
    $('#content').after(newdiv);
});

然后在 php 中你会得到 $_POST["name_string"] ,它类似于 "name1,name2,name3,"。您可以在 php 中使用explode 函数从该字符串创建和排列:

$array = explode(",", $_POST["name_string"]);
于 2015-08-19T11:01:25.093 回答