PHP表单处理,复选框数组问题

PHP表单处理,复选框数组问题,php,Php,我有一个简单的PHP脚本来处理HTML表单,除了多个复选框输入之外,所有内容都正确地转储到电子邮件中。它不显示用户选择的复选框,而是显示数组 <?php if($_SERVER['REQUEST_METHOD'] !== 'POST') { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; die; } $fi

我有一个简单的PHP脚本来处理HTML表单,除了多个复选框输入之外,所有内容都正确地转储到电子邮件中。它不显示用户选择的复选框,而是显示数组

<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
die;
}


$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $initial = $_POST['initial']; $streetaddress = $_POST['streetaddress']; $addressline2 = $_POST['addressline2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $gender = $_POST['gender']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $shirtsize = $_POST['shirt_size']; $country = $_POST['country']; $dayarea = $_POST['daytime_phone_area']; $dayphone = $_POST['daytime_phone']; $shirtsizeother = $_POST['other']; $dates = $_POST['dates']; $signature = $_POST['signature']; $signday = $_POST['signature_day']; $signmonth = $_POST['signature_month']; $signyear = $_POST['signature_year']; $position = $_POST['position']; $contactmethod = $_POST['contactmethod']; $other_size = 'n/a'; $_POST['other_size'] = 'n/a'; 

//Validate first



/*
Simple form validation
check to see if required fields were entered */ if ($_POST['firstname'] == "" || $_POST['lastname'] == "" || $_POST['streetaddress'] == "" || $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['email'] == "" || $_POST['month'] == "" || $_POST['day'] == "" || $_POST['year'] == "" || $_POST['gender'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['daytime_phone_area'] == ""  || $_POST['daytime_phone'] == ""  || $_POST['dates'] == ""  || $_POST['signature'] == ""  || $_POST['signature_day'] == ""  || $_POST['signature_month'] == ""  || $_POST['shirt_size'] == ""  || $_POST['signature_year'] == ""  || $_POST['position'] == ""  || $_POST['contactmethod'] == "" ) {
    var_dump($_POST);
    echo "Please fill in all required boxes.";
    }
else {


$email_from = 'chris569x@gmail.com';//<== update the email address $email_subject = "New Volunteer Registration"; $email_body = "You have received a new volunteer registration.\n". 
"Volunteer: $firstname $initial $lastname \n".
"Address: $streetaddress \n".
"$addressline2 \n".
"$city, $state $zip \n".
"Email: $email \n".
"Date of Birth: $month/$day/$year \n".
"Gender: $gender \n".
"Cell Phone: ($areacode) $cellphone \n".
"Daytime Phone: ($dayarea) $dayphone \n".
"T-Shirt Size: $shirtsize $shirtsizeother \n".
"Volunteer Position: $position \n".
"Dates availible to volunteer: $dates \n".
"Electronic Signature: $signature $signmonth/$signday/$signyear \n".
"Preferred contact method: $contactmethod \n"; $to = "chris569x@gmail.com";//<== update the email address $headers = "From: $email_from \r\n"; //Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thanks2.htm');
echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>";
}
?> 
尝试使用PHP函数并更改

$dates = $_POST['dates'];

这将把数组转换成一个字符串,其中每个日期用逗号分隔。

尝试使用PHP函数并更改

$dates = $_POST['dates'];


这将把数组转换成一个字符串,其中每个日期用逗号分隔。

在HTML表单中使用名称
dates[]
时,您指定数据应采用数组格式。
[]
语法告诉表单在POST数据数组中创建新元素

要逐步遍历数组的值,可以使用
foreach

foreach($dates as $v) {
    echo $v . "<br />";
}
foreach($v日期){
回声$v.“
”; }
在HTML表单中使用名称
日期[]
时,您指定数据应采用数组格式。
[]
语法告诉表单在POST数据数组中创建新元素

要逐步遍历数组的值,可以使用
foreach

foreach($dates as $v) {
    echo $v . "<br />";
}
foreach($v日期){
回声$v.“
”; }