1

我创建了一个简单的 HTML 表单,并使用 PHP 处理它。

当我尝试提交表单时,它给了我我创建的错误消息,因为我没有填写所有必需的框。我已经检查并重新检查了变量,但我无法弄清楚为什么它会向我发送所有字段都已填写的消息。

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
<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"> 
<option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>

</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"> Small
<input type="radio" name="shirt_size" value="Medium"> Medium
<input type="radio" name="shirt_size" value="Large"> Large
<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 />
<label>Other:</label><input type="text" name="other size" /><br /><br />
<p>In which area will you be helping*:</p>
<select>
<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">July 6
<input type="checkbox" name="dates" value="7/7" />July 7
<input type="checkbox" name="dates" value="7/8">July 8
<input type="checkbox" name="dates" value="7/9">July 9
<input type="checkbox" name="dates" value="7/10">July 10
<input type="checkbox" name="dates" value="7/11">July 11
<input type="checkbox" name="dates" value="7/12">July 12<br /><br /><br /><br />
<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>

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']; 

//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'] == "" ) {
    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".
"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'>";
}
?> 
4

2 回答 2

1

我相信您需要更改您的职位选择。

似乎该名称没有出现在帖子页面上,因为您输入的是名称而不是 . 解决这个问题,你应该很高兴。

于 2013-01-28T17:59:00.133 回答
0

恭喜,您的服务器似乎配置正确!变量 $city 不应包含任何数据。您必须改用 $_POST['city'] 或 $_REQUEST['city'] 。

一些主机可能仍然支持直接向 $variables 提交变量,但这不是一个安全的配置。

例如:

... if (password_check($username,$password)) { $access = 'yes' } ...

if ('yes'==$access) { ... }

可以通过提交 script.php&access=yes 被黑

所以你必须直接使用 $_POST['variable'] 或者首先将它复制到 $variable 中。

于 2013-01-28T17:53:38.173 回答