0

HTMLフォームを処理する単純なPHPスクリプトがあり、複数のチェックボックス入力を除いて、すべてが正しく電子メールにダンプされます。ユーザーが選択したチェックボックスを表示する代わりに、配列を表示します。

<?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'>";
}
?> 

および電子メール出力(問題は、ボランティアに利用できる日付にあります):

新しいボランティア登録を受け取りました。ボランティア: xxxxxx 住所: xxxx

xxxxxx メールアドレス: xxxx 生年月日: xxxx 性別: xxxx 携帯電話番号: xxxx 昼間の電話番号: xxxx T シャツのサイズ: xxxx

ボランティア可能日: Array

電子署名: xxxx ご希望の連絡方法: xxxx

必要な場合は、HTML フォームを次に示します。

<form name="volunteer" action="form-to-email3.php" method="post">

<label>Last Name*: </label><input type="text" name="lastname" /><br /> <label>First Name*: </label><input type="text" name="firstname" /><br /> <label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br /> <label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)"/>
<SCRIPT TYPE="text/javascript">
<!--
autojump("month", "day", 2); autojump("day", "year", 2); //--> </SCRIPT><br /><br /> <label>Gender*:</label><input type="radio" name="gender" value="Male"> Male <input type="radio" name="gender" value="Female"> Female </input> <br /> <br /> <label>Street Address*: </label><input type="text" name="streetaddress" /><br /> <label>Address Line 2: </label><input type="text" name="addressline2" /><br />
<label>City*: </label><input type="text" name="city" /><br />
<label>State/Province/Region*: </label><input type="text" name="state" /><br />
<label>Zipcode*: </label>
<INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)"/><br />
<label>Country: </label>
<select class="element select medium" id="element_3_6" name="country"> 
</select> <br /><br />

<label>Email*: </label><input type="email" name="email" /><br /><br />

<label>Cell Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)"/>)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"/><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("areacode", "cellphone", 3);
//-->
</SCRIPT>
<label>Daytime Phone*: </label>(<INPUT NAME="daytime_phone_area" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)"/>)<INPUT NAME="daytime_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"/><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("daytime_phone_area", "daytime_phone", 3); //--> </SCRIPT>

<label>T-Shirt Size*: </label><input type="radio" name="shirt_size" value="Small"> S <input type="radio" name="shirt_size" value="Medium"> M <input type="radio" name="shirt_size" value="Large"> L <input type="radio" name="shirt_size" value="XL"> XL <input type="radio" name="shirt_size" value="2XL"> 2XL <input type="radio" name="shirt_size" value="Other"> Other (Please provide)<br /> </input>
<label>Other: </label><input type="text" name="other size" /><br /><br /> <label>In which area will you be helping*: </label> <select name="position"> <option name="position" value= "Pastor">Pastor</option> <option name="position" value="Nursing Staff">Nursing Staff</option> <option name="position" value="Camp Staff">Camp Staff (Including Huddle Leaders)</option> <option name="position" value="Other Staff">Other Staff</option> </select><br /><br /> <label>Dates Attending*: <br />(Please check all dates you can be present)</label> <input type="checkbox" name="dates[]" value="7/6">Jul. 6 <input type="checkbox" name="dates[]" value="7/7">Jul. 7 <input type="checkbox" name="dates[]" value="7/8">Jul. 8 <input type="checkbox" name="dates[]" value="7/9">Jul. 9 <input type="checkbox" name="dates[]" value="7/10">Jul. 10 <input type="checkbox" name="dates[]" value="7/11">Jul. 11 <input type="checkbox" name="dates[]" value="7/12">Jul. 12<br /><br /><br /><br /></input> <label>What is the best way to contact you*? </label><input type="text" name="contactmethod" /><br /><br /><br />

<b>Electronic Submission</b>
<p>I understand that by filling in my name here, it shall act as a binding, legal signagure. </p> <label>Electronic Signagure*: </label><input type="text" name="signature" /><br />
<label>Date*: </label><INPUT NAME="signature_month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="signature_day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="signature_year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)"/>
<SCRIPT TYPE="text/javascript">
<!--
autojump("signature_month", "signature_day", 2); autojump("signature_day", "signature_year", 2); //--> </SCRIPT><br /><br /><br /><br /><br />


<input type="submit" value="Submit" />
</form>
4

2 に答える 2

3

PHP のimplode()関数を使用して変更してみてください

$dates = $_POST['dates'];

$dates = implode(", ",$_POST['dates']);

これにより、配列が各日付がカンマで区切られた文字列に変換されます。

于 2013-02-01T16:32:11.927 に答える
0

HTMLフォームで名前を使用するときにdates[]、データを配列形式にする必要があることを指定しました。構文は、POSTデータの[]配列に新しい要素を作成するようにフォームに指示します。

配列の値をステップスルーするには、次を使用しますforeach

foreach($dates as $v) {
    echo $v . "<br />";
}
于 2013-02-01T16:36:53.463 に答える