如何在php中的单个变量中添加$ _POST多次

时间:2016-05-08 05:07:49

标签: php variables global

我正在使用邮件功能接收来自客户的电子邮件,并将提交的数据直接显示在我的电子邮件中。 如何在单个变量中添加多个全局变量$_POST[''],因为以下代码不起作用:

$message = $first_name . " " . $last_name . " details are:" . "\n\n\n\n" . $_POST['father_name'],$_POST['dob'],$_POST['email'],$_POST['address'],$_POST['contact_number'];

3 个答案:

答案 0 :(得分:0)

我假设您要添加逗号分隔符,请使用此

$message = $first_name . " " .  $last_name . " details are:" . "\n\n\n\n" . "{$_POST['father_name']},{$_POST['dob']},{$_POST['email']},{$_POST['address']},{$_POST['contact_number']}";

答案 1 :(得分:0)

PHP语法不使用逗号(如perl),您需要使用以下内容。并始终过滤您的输入!

$filtered = filter_input_array(INPUT_POST, [
  "father_name"    => FILTER_SANITIZE_STRING,
  "dob"            => FILTER_SANITIZE_STRING,
  "email"          => FILTER_VALIDATE_EMAIL,
  "address"        => FILTER_SANITIZE_STRING,
  "contact_number" => FILTER_SANITIZE_STRING
]);
$message  = $first_name . " " . $last_name . " details are:" . "\n\n\n\n";
$message .= $filtered['father_name'];
$message .= $filtered['dob'];
$message .= $filtered['email'] ?: "invalid email";
$message .= $filtered['address'];
$message .= $filtered['contact_number'];

如果您想要换行,请在分号前的每个连接字段中附加. "\n"。如果你想要html list-items(你的后续评论对我来说似乎不清楚),请在<li>标记中包装每个连接字段:

即,

// ... (see above)
$message .= "<li>" . $filtered['father_name'] . "</li>";
// ... (same for other fields)

答案 2 :(得分:0)

尝试简单的连接 $message = $first_name . " " . $last_name . " details are:" . "\n\n\n\n" . $_POST['father_name'].','.$_POST['dob'].','.$_POST['email'].','.$_POST['address'].','.$_POST['contact_number'];