抱歉,如果这看起来像一个愚蠢的问题,但我真的被卡住了。
我正在使用带有php的表单,允许用户从我的网站发送邮件,一切正常,除非您要添加多个“项目”,“金额< / em>'和' 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');
}
?>
答案 0 :(得分:0)
当你点击$(“#copy”时,你创建一个包含信息的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"]);
答案 1 :(得分:0)
您遇到的问题是您没有制作存储多个值的元素数组。你的脚本只是重新创建一个div。所以简单的解决方案是在你的金额,主题和盒子元素中添加“[]”。即
<input type="text" name="amount[]" />
<select name="items[]">...</select>
<select name="boxeach[]">...</select>
比你的php代码更改
中的这些行$amount = $_POST['amount'];
$items = $_POST['items'];
$boxeach = $_POST['boxeach'];
到
$amount = implode(',',$_POST['amount']);
$items = implode(',',$_POST['items']);
$boxeach = implode(',',$_POST['boxeach']);
或以您喜欢的方式操纵发布的数组。
我希望我帮助你。