我检查了很多问题和论坛,但我不知道如何通过电子邮件发送数组。
这将是产品的订购单。用户可以通过 jquery 添加新的订单行。
所以这是我的简短示例;
表单.php
<input type="text" name="array[][id]" />
<input type="text" name="array[][partno]" />
在 email.php 上;
我可以看到,它通过发布所有数组
<?php var_dump($_POST['array']); ?>
但我不知道如何通过电子邮件发送。
这是我在 email.php 中尝试的错误;
$id = $_POST['array'][]['id'];
$partno = $_POST['array'][]['partno'];
和
$email_message .= "Id: ".clean_string($id)."\n";
$email_message .= "Part No: ".clean_string($partno)."\n";
我应该怎么做才能发送该数组中的值
编辑:
我试过了,但我不知道在哪里放置@onetrickpony 代码。这是 email.php 的完整代码
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "a@a.com";
$email_subject = "order";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['array'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<?php var_dump($_POST['array']); ?>
<?php
}
?>
解决方案
据我了解,没有必要使用array[][input_name]
..
我将输入名称更改为;
"partno[]" ..etc
还添加了
!isset($_POST['partno']) || ..etc and,
$partno = $_POST['partno']; .. etc
到必要的地方,
并移除;
!isset($_POST['array'])
和关键部分;
foreach( $partno as $key => $no ) {
$email_message .= $no;
$email_message .= $partnumber[$key];
$email_message .= $quantity[$key];
$email_message .= $partname[$key]."\n";
}
根据@Jeffrey Blake 在问题主题中的回答;
如何将表单输入数组转换为 PHP 数组