2011-10-06 116 views
1

我不知道我做錯了什麼。我一直在尋找其他論壇,說我的問題可能與沒有關閉花括號關閉或簡短的PHP標記<? ...有關。據我所知,我沒有這些。這是一個表單,可讓您知道是否有任何字段留空。

<?php 

if (count($_POST) > 0) 
{ 

    function check_if_field_submitted($field_to_check) 
    { 
     if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '') 
     { 
      return TRUE; 
     } 
     else 
     { 
      return "YOU MUST FILL IN THE $field_to_check FIELD!"; 
     } 
    } 

    //-------------------------------------------------------- 

    $error_messages = array(); 

    //Validate the input 

    //Trim the fields 
    $_POST['first_name'] = trim($_POST['first_name']); 
    $_POST['last_name'] = trim($_POST['last_name']); 
    $_POST['comments'] = trim($_POST['comments']); 
    $_POST['first_name'] = strip_tags($_POST['first_name']); 
    $_POST['last_name'] = strip_tags($_POST['last_name']); 
    $_POST['comments'] = strip_tags($_POST['comments']); 

    //Required fields: 

    if (check_if_field_submitted('first_name') !== TRUE) 
    { 
     $error_messages[] = check_if_field_submitted('first_name'); 
    } 

    if (check_if_field_submitted('last_name') !== TRUE) 
    { 
     $error_messages[] = check_if_field_submitted('last_name');  
    } 

    if (check_if_field_submitted('hobbies') !== TRUE) 
    { 
     $error_messages[] = check_if_field_submitted('hobbies'); 
    } 

    if (check_if_field_submitted('university') !== TRUE) 
    { 
     $error_messages[] = check_if_field_submitted('university'); 
    } 

    if (check_if_field_submitted('year') !== TRUE) 
    { 
     $error_messages[] = check_if_field_submitted('year'); 
    } 

    if (check_if_field_submitted('comments') !== TRUE) 
    { 
     $error_messages[] = check_if_field_submitted('comments'); 

    if (count($error_messages) < 1) 
    { 
     header("Location: success.php"); 
    } 
} 


?> 


<DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
    </head> 
    <body> 

     <?php 

     if (isset($error_messages) && count($error_messages) > 0) 
     { 
      echo "<ul>"; 
      foreach($error_messages as $message) 
      { 
       echo "<li>$message</li>"; 
      } 
      echo "</ul>"; 
     }   

     ?> 

     <h1>Register or Fail</h1> 

     <form method="post" action="index.php"> 

      <fieldset> 

       <label>First Name</label> 
       <input type='text' name='first_name' value="<?php if(isset($_POST['first_name'])) { echo $_POST['first_name']; } ;?>" /> 

       <label>Last Name</label> 
       <input type='text' name='last_name' value='<?php if(isset($_POST['last_name'])) { echo $_POST['last_name']; } ;?>'/> 

      </fieldset> 

      <fieldset>    
       <label>What are your hobbies?</label> 

        <input type="checkbox" name="hobbies" value="movies" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'movies') { echo "checked='checked'"; } ?> /> Movies 
        <input type="checkbox" name="hobbies" value="sports" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'sports') { echo "checked='checked'"; } ?> /> Sports 
        <input type="checkbox" name="hobbies" value="books" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'books') { echo "checked='checked'"; } ?> /> Books 
        <input type="checkbox" name="hobbies" value="vgames" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'vgames') { echo "checked='checked'"; } ?> /> Video Games 
        <input type="checkbox" name="hobbies" value="science" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'science') { echo "checked='checked'"; } ?> /> FOR SCIENCE! 

      </fieldset> 

      <fieldset> 

       <label>What year are you?</label> 
       <input type="radio" name="year" value="1" <?php if(isset($_POST['year']) && $_POST['year'] == '1') { echo "checked='checked'"; } ?> /> Freshman 
       <input type="radio" name="year" value="2" <?php if(isset($_POST['year']) && $_POST['year'] == '2') { echo "checked='checked'"; } ?> /> Sophomore 
       <input type="radio" name="year" value="3" <?php if(isset($_POST['year']) && $_POST['year'] == '3') { echo "checked='checked'"; } ?> /> Junior 
       <input type="radio" name="year" value="4" <?php if(isset($_POST['year']) && $_POST['year'] == '4') { echo "checked='checked'"; } ?> /> Senior 
      </fieldset> 

      <fieldset> 

       <label>What university are you attending?</label> 

       <select name="university"> 
        <option value="" <?php if(isset($_POST['university']) && $_POST['university'] == '') { echo "selected='selected'"; } ?> >Please Select an Option</option> 
        <option value="1" <?php if(isset($_POST['university']) && $_POST['university'] == '1') { echo "selected='selected'"; } ?> >Florida State University</option> 
        <option value="2" <?php if(isset($_POST['university']) && $_POST['university'] == '2') { echo "selected='selected'"; } ?> >University of Florida</option> 
        <option value="3" <?php if(isset($_POST['university']) && $_POST['university'] == '3') { echo "selected='selected'"; } ?> >University of Central Florida</option> 
        <option value="4" <?php if(isset($_POST['university']) && $_POST['university'] == '4') { echo "selected='selected'"; } ?> >University of Miami</option> 
       </select> 

      </fieldset> 

      <fieldset> 
       <button type="submit">Submit</button> 
      </fieldset> 

     </form> 
    </body> 
</html> 
+0

首先,您使用的是標記html的兩倍,如您所見: 此外,您可以在第一個函數中定義一個函數,如果它不太適合編寫清晰的代碼。 –

+0

我正在計算11「{」和只有10「}」,所以是一個捲曲{不關閉... – danii

+1

你應該檢查出Notepad ++,它具有諸如匹配大括號突出顯示等功能。 –

回答

1

它看起來像你在你的if語句的一個缺少一個右括號。

我想你想改變這些行:

 if (check_if_field_submitted('comments') !== TRUE) 
     { 
      $error_messages[] = check_if_field_submitted('comments'); 

這樣:

 if (check_if_field_submitted('comments') !== TRUE) 
     { 
      $error_messages[] = check_if_field_submitted('comments'); 
     } 
3

問題:

if (check_if_field_submitted('comments') !== TRUE) 
     { 
      $error_messages[] = check_if_field_submitted('comments'); 




if (count($error_messages) < 1) 
    { 

     header("Location: success.php"); 
    } 

} 

答:

if (check_if_field_submitted('comments') !== TRUE) 
      { 
       $error_messages[] = check_if_field_submitted('comments'); 

      } 


    if (count($error_messages) < 1) 
     { 

      header("Location: success.php"); 
     } 

    } 

如果我是你,我會找到一個支架匹配/突出顯示的IDE。

+0

對IDE進行括號匹配 – danii

相關問題