2013-03-14 68 views
0

我有一個HTML表單:PHP POST表單驗證錯誤

<div id="registration"> 
    <p> SGU is for youth who have completed 6th through 12th grades. Please complete this entire form for EACH CAMPER. (<a href="PDFs/SGU_Registration_Form_2013.pdf">Download Form to Print</a>)<br /> 
    <br /> 
    * Denotes required field</p> 
    <form action="form-to-email.php" method="post" name="camper_registration" id="camper registration"> 

    <label>Last Name*: </label> 
    <input name="lastname" type="text" id="lastname" required="required"/> 
    <br /> 
    <label>First Name*: </label><input name="firstname" type="text" id="firstname" required="required"/><br /> 
    <label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br /> 

    <label>Street Address*: </label><input name="streetaddress" type="text" id="streetaddress" required="required"/><br /> 
    <label>Address Line 2: </label><input type="text" name="addressline2" /><br /> 
    <label>City*: </label><input name="city" type="text" id="city" required="required"/><br /> 
    <label>State/Province/Region*: </label><input name="state" type="text" id="state" required="required"/><br /> 
    <label>Zipcode*: </label> 
    <INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5 
    onKeyPress="return numbersonly(this, event)" required="required"><br /><br /> 
    <label>Youth's Email*: </label><input type="email" name="email" id="email" required="required"/><br /><br /> 
    <label>Youth's Cell*: </label>(<input type="tel" name="youth_area" size="3" maxlength="3" onKeyPress="return numbersonly(this, event)" required="required" />) <input type="tel" name="youth_phone" size="7" maxlength="7" onKeyPress="return numbersonly(this, event)" required="required" /><SCRIPT TYPE="text/javascript"> 
    <!-- 
    autojump("youth_area", "youth_phone", 3); 
    //--> 
    </SCRIPT><br /><br /> 
    <label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2 
    onKeyPress="return numbersonly(this, event)" required="required">/ 
    <INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2 
    onKeyPress="return numbersonly(this, event)" required="required">/ 
    <INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4 
    onKeyPress="return numbersonly(this, event)" required="required"> 
     <SCRIPT TYPE="text/javascript"> 
     autojump("month", "day", 2); autojump("day", "year", 2); 
     </SCRIPT><br /><br /> 
    <label>Grade completed in<br /> Spring 2013*: </label><input type="tel" name="grade" size=2 maxlength=2 required="required"/><br /><br /> 
    <label>Gender*:</label><input type="radio" name="gender" value="Male" required="required"> Male 
    <input type="radio" name="gender" value="Female" required="required"> Female 
    <br /> <br /> 
    <label>Parent/Guardian(s) First Name*: </label><input name="guardian_first" type="text" id="guardian_first" required="required"/><br /><br /> 
    <label>Parent/Guardian(s) Last Name*: </label><input name="guardian_last" type="text" id="guardian_last" required="required"/> <br /><br /> 
    <label>Parent Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3 
    onKeyPress="return numbersonly(this, event)" required="required">)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7 
    onKeyPress="return numbersonly(this, event)" required="required"><br /> 
    <SCRIPT TYPE="text/javascript"> 
    <!-- 
    autojump("areacode", "cellphone", 3); 
    //--> 
    </SCRIPT> 
    <label>Parent/Guardian Email*: </label><input name="parent_email" type="email" id="parent_email" required="required"/><br /> 
    <b>This email will be used for confirmation of <br /> 
    registration and all future contact</b><br /><br /> 
    <label>1st Emergency Contact*: </label><input name="emergency_contact_1" type="text" id="emergency_contact_1" required="required"/><br /> 
    <label>Contact Number*: </label>(<INPUT NAME="emergency_contact_1_areacode" input type="tel" SIZE=3 MAXLENGTH=3 
    onKeyPress="return numbersonly(this, event)" required="required">)<INPUT NAME="emergency_contact_1_phone" input type="tel"SIZE=7 MAXLENGTH=7 
    onKeyPress="return numbersonly(this, event)" required="required"><br /><br /> 
    <SCRIPT TYPE="text/javascript"> 
    <!-- 
    autojump("emergency_contact_1_areacode", "emergency_contact_1_phone", 3); 
    //--> 
    </SCRIPT> 
    <label>2nd Emergency Contact*: </label><input name="emergency_contact_2" type="text" id="emergency_contact_2" required="required"/><br /> 
    <label>Contact Number*: </label>(<INPUT NAME="emergency_contact_2_areacode" input type="tel" SIZE=3 MAXLENGTH=3 
    onKeyPress="return numbersonly(this, event)" required="required">)<INPUT NAME="emergency_contact_2_phone" input type="tel"SIZE=7 MAXLENGTH=7 
    onKeyPress="return numbersonly(this, event)" required="required"><br /><br /> 
     <SCRIPT TYPE="text/javascript"> 
    <!-- 
    autojump("emergency_contact_2_areacode", "emergency_contact_2_phone", 3); 
    //--> 
    </SCRIPT> 
    <label>Name of Home Church: </label><input type="text" name="home_church" /><br /> 
    <label>Phone Number: </label>(<INPUT NAME="church_areacode" input type="tel" SIZE=3 MAXLENGTH=3 
    onKeyPress="return numbersonly(this, event)">)<INPUT NAME="church_phone" input type="tel"SIZE=7 MAXLENGTH=7 
    onKeyPress="return numbersonly(this, event)"><br /> 
     <SCRIPT TYPE="text/javascript"> 
    autojump("church_areacode", "church_phone", 3); 
    </SCRIPT> 
    <label>Contact Person: </label><input type="text" name="contact_person" /><br /><br /> 
    <b>Special Needs</b><br /> 
    Some campers may have needs that might require special attention from our staff; accessibility, health concerns, diet, allergies, etc. <br /><br /> 
    <label>Please list any special needs: </label> 
    <textarea rows="10" cols="20" name="special_needs"> 
    </textarea> <br /><br /> 
    <label>T-Shirt Size*: </label><input type="radio" name="shirt size" value="Small" required="required"> Small 
    <input type="radio" name="shirt size" value="Medium" required="required"> Medium 
    <input type="radio" name="shirt size" value="Large" required="required"> Large 
    <input type="radio" name="shirt size" value="XL" required="required"> XL 
    <input type="radio" name="shirt size" value="2XL" required="required"> 2XL<br /><br /> 
    <b>Roommate</b><br /> 
    There are double and many single occupancy dorm rooms at Grinnell college campus - if possible we will honor your request for ONE preferred roommate.<br /><br /> 
    <label>Roommate Preference: </label> 
    <input type="text" name="roommate" /><br /><br /> 
    <div id="satellites"> 
     <p><b>Satellite Choices</b> 
      List your first, second, and third choices. You will be given your first choice if it is not full. ALL events have limited capacity. If you do not choose a satellite, one will be assigned for you.<br /><br /> 
      <label>First Choice*: </label> 
      <select name="satellite1" id="satellite1" required="required" onchange="clearOthers()"> 
      <option value="" disabled>Select</option> 
      <option value="art">Art</option> 
      <option value="basketball">Basketball</option> 
      <option value="dance">Dance</option> 
      <option value="drama">Drama</option> 
      <option value="football">Football</option> 
      <option value="missions">Missions</option> 
      <option value="photo1">Landscape & Still Life Photography</option> 
      <option value="photo2">People Photography</option> 
      <option value="scrap-booking">Scrap-Booking</option> 
      <option value="sgjr">Summer Games Jr. Training</option> 
      <option value="soccer">Soccer</option> 
      <option value="ultimate">Ultimate Frisbee</option> 
      <option value="video_games">Video Games</option> 
      <option value="volleyball">Volleyball</option> 
      <option value="water_park">Water Park</option> 
      </select><br /> 


      <label>Second Choice*: </label> 
      <select name="satellite2" id="satellite2" required="required"> 
      <option value="" disabled>Select</option> 
      <option value="art">Art</option> 
      <option value="basketball">Basketball</option> 
      <option value="dance">Dance</option> 
      <option value="drama">Drama</option> 
      <option value="football">Football</option> 
      <option value="missions">Missions</option> 
      <option value="photo1">Landscape & Still Life Photography</option> 
      <option value="photo2">People Photography</option> 
      <option value="scrap-booking">Scrap-Booking</option> 
      <option value="sgjr">Summer Games Jr. Training</option> 
      <option value="soccer">Soccer</option> 
      <option value="ultimate">Ultimate Frisbee</option> 
      <option value="video_games">Video Games</option> 
      <option value="volleyball">Volleyball</option> 
      <option value="water_park">Water Park</option> 
      </select><br /> 



      <label>Third Choice*: </label> 
      <select name="satellite3" id="satellite3" required="required"> 
      <option value="" disabled>Select</option> 
      <option value="art">Art</option> 
      <option value="basketball">Basketball</option> 
      <option value="dance">Dance</option> 
      <option value="drama">Drama</option> 
      <option value="football">Football</option> 
      <option value="missions">Missions</option> 
      <option value="photo1">Landscape & Still Life Photography</option> 
      <option value="photo2">People Photography</option> 
      <option value="scrap-booking">Scrap-Booking</option> 
      <option value="sgjr">Summer Games Jr. Training</option> 
      <option value="soccer">Soccer</option> 
      <option value="ultimate">Ultimate Frisbee</option> 
      <option value="video_games">Video Games</option> 
      <option value="volleyball">Volleyball</option> 
      <option value="water_park">Water Park</option> 
     </select> 
    <script type="text/javascript"> 
    function clearOthers() { 
    document.getElementById("secondchoice").options.selectedIndex=0; 
    document.getElementById("thirdchoice").options.selectedIndex=0; 
    } 
    </script>  
    </div> 

然後用這個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']; $grade = $_POST['grade']; $gender = $_POST['gender']; $guardian_first = $_POST['guardian_first']; $guardian_last = $_POST['guardian_last']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $contact1 = $_POST['emergency_contact_1']; $contact1areacode = $_POST['emergency_contact_1_areacode']; $contact1phone = $_POST['emergency_contact_1_phone']; $contact2 = $_POST['emergency_contact_2']; $contact2areacode = $_POST['emergency_contact_2_areacode']; $contact2phone = $_POST['emergency_contact_2_phone']; $homechurch = $_POST['home_church']; $churchareacode = $_POST['church_areacode']; $churchphone = $_POST['church_phone']; $contactperson = $_POST['contact_person']; $specialneeds = $_POST['special_needs']; $shirtsize = $_POST['shirt size']; $roommate = $_POST['roommate']; $firstchoice = $_POST['satellite1']; $secondchoice = $_POST['satellite2']; $thirdchoice = $_POST['satellite3']; $youth_area = $_POST['youth_area']; $youth_phone = $_POST['youth_phone']; 

    //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['grade'] == "" || $_POST['gender'] == "" || $_POST['guardian_first'] == "" || $_POST['guardian_last'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['emergency_contact_1'] == "" || $_POST['emergency_contact_1_areacode'] == "" || $_POST['emergency_contact_1_phone'] == "" || $_POST['emergency_contact_2'] == "" || $_POST['emergency_contact_2_areacode'] == "" || $_POST['emergency_contact_2_phone'] == "" || $_POST['shirt size'] == "" || $_POST['satellite1'] == "" || $_POST['satellite2'] == "" || $_POST['satellite3'] == "" || $_POST['youth_area'] == "" || $_POST['youth_phone'] == "") { 
      echo "Please fill in all required boxes. \n"; 
      var_dump($_POST);} 
     else { 


    $email_from = '[email protected]';//<== update the email address 
    $email_subject = "New Registration"; 
    $email_body = "You have received a new registration.\n". 
    "Camper: $firstname $initial $lastname \n". 
    "Address: $streetaddress \n". 
    "$addressline2 \n". 
    "$city, $state $zip \n". 
    "Email: $email \n". 
    "Date of Birth: $month/$day/$year \n". 
    "Grade Completed: $grade \n". 
    "Gender: $gender \n". 
    "Youth Cell Phone: ($youth_area) $youth_phone \n". 
    "Guardian: $guardian_first $guardian_last \n". 
    "Guardian Cell Phone: ($areacode) $cellphone \n". 
    "First Emergency Contact: $contact1 Contact Number: ($contact1areacode) $contact1phone \n". 
    "Second Emergency Contact: $contact2 Contact Number: ($contact2areacode) $contact2phone \n". 
    "Home Church: $homechurch Contact Number: ($churchareacode) $churchphone Contact Person: $contactperson \n". 
    "Special Needs: $specialneeds \n". 
    "T-Shirt Size: $shirtsize \n". 
    "Roommate Preference: $roommate \n". 
    "Satellite Preferences: 1.$firstchoice 2.$secondchoice 3.$thirdchoice \n". 
    " \n"; 
    $to = "[email protected],[email protected]";//<== 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",303); 
    echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>"; 
    } 
    ?> 

我包括後續代碼var_dump因爲我不斷收到我的「請所有填寫需要的箱子「。錯誤。當我查看轉儲時,沒有沒有價值的字符串。我很困惑,斷開連接在哪裏?

var_dump:

請填寫所有必填框。 array(38){[「lastname」] => string(4)「test」[「firstname」] => string(4)「test」[「initial」] => string(1)「t」[「streetaddress 「」=「string」(8)「123 test」[「addressline2」] => string(4)「test」[「city」] => string(9)「testville」[「state」] => string(10 )「test state」[「zip」] => string(5)「12345」[「email」] => string(13)「[email protected]」[「youth_area」] => string(3)「123 「[」youth_phone「] => string(7)」1234566「[」month「] => string(2)」21「[」day「] => string(2)」21「[」year「] =>字符串(4)「2121」[「grade」] => string(2)「12」[「gender」] => string(4)「Male」[「guardian_first」] => string(9) [「guardian_last」] => string(9)「test test」[「areacode」] => string(3)「123」[「cellphone」] => string(7)「1231231」[「parent_email」] => string(13)「[email protected]」[「emergency_contact_1」] => string(9)「test test」[「emergency_contact_1_areacode」] => string(3)「123」[「emergency_contact_1_phone」] => string(7) )「1231231」[「emergency_contact_2」] => string(10)「test testy」[「emergency_contact_2_areacode」] => string(3)「123」[「emergency_contact_2_ph一個「] => string(7)」1232132「[」home_church「] => string(11)」church test「[」church_areacode「] => string(3)」321「[」church_phone「] => string 7)「6548976」[「contact_person」] => string(12)「tester tests」[「special_needs」] => string(13)「none。測試「[」shirt_size「] =>字符串(2)」XL「[」roommate「] =>字符串(4)」none「[」satellite1「] =>字符串(5)」drama「[」satellite2「] = >串(6) 「照片1」[ 「satellite3」] =>串(10) 「排球」[ 「提交」] =>串(6) 「提交」}

回答

1

你錯過了,如果下劃線:

if ($_POST['firstname'] == "" || 

$_POST['shirt size']應該shirt_size。
                                                                                                                                                 ^


這發生在你身上,因爲你的做法是不優雅,那巨大的If是討厭討厭討厭的xD。你應該看看foreach()學習循環數組。在php文檔中也可以看到empty()isset() ,這些是你驗證的第一個朋友。


這怎麼做:

Basicly搜索你的錯誤,我複製粘貼你的if語句,以及var_dumped陣列,使用了一些正則表達式魔術上他們,使他們成爲有效的PHP數組:

$arr = array("lastname"=>"test", "firstname"=>"test", "initial"=>"t", "streetaddress"=>"123 test", "addressline2"=>"test", "city"=>"testville", "state"=>"test state", "zip"=>"12345", "email"=>"[email protected]", "youth_area"=>"123", "youth_phone"=>"1234566", "month"=>"21", "day"=>"21", "year"=>"2121", "grade"=>"12", "gender"=>"Male", "guardian_first"=>"test test", "guardian_last"=>"test test", "areacode"=>"123", "cellphone"=>"1231231", "parent_email"=>"[email protected]", "emergency_contact_1"=>"test test", "emergency_contact_1_areacode"=>"123", "emergency_contact_1_phone"=>"1231231", "emergency_contact_2"=>"test testy", "emergency_contact_2_areacode"=>"123", "emergency_contact_2_phone"=>"1232132", "home_church"=>"church test", "church_areacode"=>"321", "church_phone"=>"6548976", "contact_person"=>"tester tests", "special_needs"=>"none. testing", "shirt_size"=>"XL", "roommate"=>"none", "satellite1"=>"drama", "satellite2"=>"photo1", "satellite3"=>"volleyball", "submit"=>"Submit"); 
$arr2 = array('firstname', 'lastname', 'streetaddress', 'city', 'state', 'zip', 'email', 'month', 'day', 'year', 'grade', 'gender', 'guardian_first', 'guardian_last', 'areacode', 'cellphone', 'emergency_contact_1', 'emergency_contact_1_areacode', 'emergency_contact_1_phone', 'emergency_contact_2', 'emergency_contact_2_areacode', 'emergency_contact_2_phone', 'shirt size', 'satellite1', 'satellite2', 'satellite3', 'youth_area', 'youth_phone'); 

下一次請使用var_export(),所以你輸出一個有效的PHP數組,人們可以只是粘貼它!

然後我用這個循環,以瞭解他們是否匹配:

foreach($arr2 as $k=>$v){ 
    if(!array_key_exists($v, $arr)){ 
     echo "$v not in array 1 <br/>"; 
    } 
} 

輸出:恤尺寸不陣列1

享受!

+0

啊,我忘了PHP不會識別這個空間。我回去了,把所有的襯衫尺碼改正爲shirt_size,一切按我的意圖進行。謝謝! – Chris569x 2013-03-14 16:52:47

+0

不用客氣,php確實可以識別空間,只是空間和下劃線是不同的東西......請記住嘗試一種不同的方法,就像我在答案中展示的那樣,例如,它會爲您節省很多時間檢查那些愚蠢的小東西 – aleation 2013-03-14 16:54:30

0
<?php 
    if (isset($_POST['submit'])) { 
     $firstname = $_POST['firstname']; 
     $lastname = $_POST['lastname']; 

     if (empty($firstname == '' || $lastname == '')) { 
      echo "empty"; 
     } 
    } 

?> 

或者財產以後這樣的:

// Required field names 
$required = array('login', 'password', 'confirm', 'name', 'phone', 'email'); 

// Loop over field names, make sure each one exists and is not empty 
$error = false; 
foreach($required as $field) { 
    if (empty($_POST[$field])) { 
    $error = true; 
    } 
} 

if ($error) { 
    echo "All fields are required."; 
} else { 
    echo "Proceed..."; 
} 
0

請使用

if(empty($_POST['firstname']) && $_POST['firstname']) 

代替:

​​

對於每個字段。您的數組非常大,您可能錯過了任何字段,empty()將檢查它是否已設置&下一條語句將檢查它是否爲非零。 for more about empty()

希望能對您有所幫助!

+0

@nOOb非常真實! – sandip 2013-03-14 16:16:06