0

我检查了很多问题和论坛,但我不知道如何通过电子邮件发送数组。

这将是产品的订购单。用户可以通过 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 数组

4

2 回答 2

3

[]用于为 PHP 中的数组赋值。

顺便说一下输入的命名方式,看起来您应该能够像这样访问它:

$id = $_POST['array'][0]['id'];
$partno = $_POST['array'][0]['partno'];

但是,如果您有多个条目,则必须对其进行迭代。就像是:

foreach($_POST['array'] as $item){

   $id = $item['id'];
   $partno = $item['partno'];

   $email_message .= "Id: ".clean_string($id)."\n";
   $email_message .= "Part No: ".clean_string($partno)."\n";
}
于 2013-07-15T21:13:45.277 回答
1

我认为您的代码应该看起来更像:

<input type='text' name='lookHere[]' />
<input type='text' name='lookHere[]' />

$lh = $_POST['lookHere'];
$email_message .= 'Id: '.htmlentities($lh[0], 3, 'UTF-8')."\n";
$email_message .= 'Part No: '.htmlentities($lh[1], 3, 'UTF-8')."\n";

注意 3 是ENT_QUOTES. 如果需要,您可以使用正确的引用样式和字符集来替换它。就个人而言,我会使用https://www.PHPglue.com上的PHPglue库。

于 2013-07-15T21:28:29.213 回答