2011-11-26 198 views
0

我正在創建一個註冊表單,但我只有一個問題......爲什麼他們可以通過無效條目驗證?唯一的事情(我知道)是他們可以通過它而不選擇一個國家!使用PHP的表單無法正常工作

這裏是我的HTML/PHP代碼:

<table border="0" width="100%"> 
    <form action="" method="post"> 
     <tr><td><label>Username:<span class="requiredfield">*</span></label></td><td width="200"><input type="text" name="myusername" id="myusername" maxlength="15" style="background-color:transparent;" /></td></tr> 
     <tr><td><label>Summoner Name (League of Legends):<span class="requiredfield">*</span></label></td><td><input type="text" name="mysummonername" id="mysummonername" maxlength="30" /></td></tr> 
     <tr><td><label>Password:<span class="requiredfield">*</span></label></td><td><input type="password" name="mypassword" id="mypassword" maxlength="15" /></td></tr> 
     <tr><td><label>Retype Password:<span class="requiredfield">*</span></label></td><td><input type="password" name="myrpassword" id="myrpassword" maxlength="15" /></td></tr> 
     <tr><td><label>E-mail:<span class="requiredfield">*</span></label></td><td><input type="text" name="myemail" id="myemail" maxlength="65" /></td></tr> 
     <tr><td><label>Retype E-mail:<span class="requiredfield">*</span></label></td><td><input type="text" name="myremail" id="myremail" maxlength="65" /></td></tr> 
     <tr><td width="300"><br />Please note that we only need this information because we'll send out prices to the winners of our tournaments.</td></tr> 
     <tr><td><label>First Name:<span class="requiredfield">*</span></label></td><td><input type="text" name="myfname" id="myfname" maxlength="20" /></td></tr> 
     <tr><td><label>Last Name:<span class="requiredfield">*</span></label></td><td><input type="text" name="mylname" id="mylname" maxlength="30" /></td></tr> 
     <tr><td><label>Select Country:<span class="requiredfield">*</span></label></td><td><select name="mycountry" id="mycountry"> 
      <option value="-choose-">-Choose-</option> 
      <option value="Afganistan">Afghanistan</option> 
      <option value="Albania">Albania</option> 
      <option value="Algeria">Algeria</option> 
      ... 
     </select></td></tr> 
     <tr><td><label>Postal Code:</label></td><td><input type="text" name="myzipcode" id="myzipcode" maxlength="9" /></td></tr> 
     <tr><td><label>City:</label></td><td><input type="text" name="mycity" id="mycity" maxlength="60" /></td></tr> 
     <tr><td><label>Street:</label></td><td><input type="text" name="mystreet" id="mystreet" maxlength="50" /></td></tr> 
     <tr><td><label>Telephone Number:</label></td><td><input type="text" name="myphonenumber" id="myphonenumber" maxlength="15" /></td></tr> 
     <!--<tr><td><label>Invite Code<span style="color:#F00;">*</span>:</label></td><td><input type="text" name="myinvcode" id="myinvcode" /></td></tr>--> 
     <!-- <tr><td colspan="2" style="height: 20px;"></td></tr> --> 
     <tr><td width="300"><br />This information will be used later on when you have lost your password and need to retrieve it again.</td></tr> 
     <tr><td><label>Secret Question:<span class="requiredfield">*</span></label></td><td><input type="text" name="myscrtquestion" id="myscrtquestion" maxlength="50" /></td></tr> 
     <tr><td><label>Secret Answer:<span class="requiredfield">*</span></label></td><td><input type="text" name="myscrtanswer" id="myscrtanswer" maxlength="50" /></td></tr> 
     <tr><td></td><td><span class="button"><input type="submit" name="submitregister" value="Register" /></span></td></tr> 
     <tr><td colspan="2">By pressing "Register" you agree to the <a href='termsofuse.php'>terms of use</a> and our <a href='privacypolicy.php'>privacy policy</a>.</td></tr> 
    </form> 

    <?php 
    if (isset($_POST['submitregister'])) { 
     ob_start(); 
     include 'config.php'; // Connect to Database 

     // Define $myusername and $mypassword 
     $myusername=$_POST['myusername']; 
     $mysummonername=$_POST['mysummonername']; 
     $myfname=$_POST['myfname']; 
     $mylname=$_POST['mylname']; 
     $mypassword=$_POST['mypassword']; 
     $myrpassword=$_POST['myrpassword']; 
     $myemail=$_POST['myemail']; 
     $myremail=$_POST['myremail']; 
     // $myinvcode=$_POST['myinvcode']; 
     $myphonenumber=$_POST['myphonenumber']; 
     $mycountry=$_POST['mycountry']; 
     $myzipcode=$_POST['myzipcode']; 
     $mycity=$_POST['mycity']; 
     $mystreet=$_POST['mystreet']; 
     $myscrtquestion=$_POST['myscrtquestion']; 
     $myscrtanswer=$_POST['myscrtanswer']; 

     // check to make sure required fields are entered 
     if ($myusername == '' || $mysummonername == '' || $myfname == '' || $mylname == '' || $mypassword == '' || $myemail == '' || $mycountry == "-Choose-" || $myscrtquestion == '' || $myscrtanswer == '') { 
      echo "<span style='color:#F00;'>Please fill in all the required fields. All the fields which are marked with a <span style='color:#0076c9;'>*</span> are required fields!</span>"; 

爲什麼你仍然可以通過這個時候你沒有帶選擇一個國家?我無法弄清楚!

+0

在未來,請的代碼只交相關位,而不是百線國家選擇下拉菜單中。 – jprofitt

+0

請記住,PHP區分大小寫。 '選擇'是另一種東西,然後'選擇'作爲PHP中的變量。 – Dorvalla

回答

4

您正在查看-Choose-但是,您的值是-choose-

+0

你知道我在哪裏或如何得到這種特殊驗證? – ZyteX

+0

我會嘗試將它改爲小寫,然後 – ZyteX

+0

它起作用了!我想到這可能是把所有東西搞砸了,但我認爲這是確切的字符串,因爲它在選項標籤中,所以我認爲它不可能是這樣。非常感謝! – ZyteX

3

因爲在選擇值時將其傳遞給國家的值爲-choose-,但是您正在檢查$mycountry == "-Choose-"。這兩個值不相等。

你或許應該開發一個更好的方法來檢查國家是否正確。任何人都可以複製表單並將其指向您的頁面,輸入他們喜歡的任何值,然後僅檢查以確保它不是而不是-choose-

1

檢查後者的外殼。在選擇你 - 請選擇 - 但你把它比作請選擇 -

1

正如前面回答說,你檢查-Choose當你需要-choose-。未來調試此問題的一種方法是執行以下操作:
print_r($_POST);
它向您顯示所有POST數據,因此您可以查看已發佈的內容。

要查看他們是否沒有選擇國家,請查看相關的發佈數據是否爲空。

1

要麼改變你的<option value="-choose-">-Choose-</option><option value="-Choose-">-Choose-</option>,或離開的「請選擇 - 」選項空白的價值和檢查空「mycountry」:$mycountry == ""

0

請選擇 - 因爲在你的HTML全部小寫的選項代碼,並且您正在測試 - 在您的PHP中使用大寫「C」進行選擇。

羅斯