2

大家好,我需要帮助!一周前我开始学习php。我有一个为联系人发布一些文本字段的表单,我需要将数组拆分为一些变量以放入电子邮件中。这是我的代码

$field  = $_POST['input'];
if ( isset( $field ) === TRUE){

foreach ($field as $key => $value) {

    echo '<pre>' , print_r( $value ) , '</pre>'; //to list the array

    }

    $to = $mail;
    $sbj = "thanks to register to $site";
    ..//some headers
    mail($to,sbj,$headers)

}

这是表格

<form action="upload.php" method="POST">
 <input type="text" name="input[]">
 <input type="text" name="input[]">
 <input type="text" name="input[]">
 <input type="submit" value="invia">
</form>

关于检索数组上的变量以包含在邮件中的任何建议?

4

3 回答 3

2

@John 给出了正确的程序。您可以使用array_push()函数来使用另一个过程,例如:

$field  = $_POST['input'];
$info =  array();

foreach ($field as $key => $value) {
 array_push($info,$value);
}

// echo implode(",",$info);
$to = $mail;
$sbj = "thanks to register to $site";
$body = implode(",",$info);
..//some headers
mail($to,sbj,$body,$headers)
于 2013-08-08T08:49:15.273 回答
1

You can use the implode() function , which takes an array and a separator and joins the elements of the array together into a string, with each element separated by the separator string you pass it eg:

foreach($field as $key=>$value) {
    $input[] = $value;
}

$body = implode("\n",$input); //implode here

$to = $mail;
$sbj = "thanks to register to $site";
..//some headers
mail($to,sbj,$body,$headers) 

This would create your list of input each on a separate line in the email.

于 2013-08-08T08:34:45.080 回答
0

最简单但最丑陋的方法就是使用:

mail($to,sbj,print_r($field, true),$headers);

print_r($field, true)会将值输出到变量而不是立即打印出来。

于 2013-08-08T08:27:21.660 回答