2015-10-19 152 views
-1

我是一名Java人員,並開始使用PHP。這是我在PHP上的第一個問題。PHP驗證 - 表單無法驗證

用形式玩耍。我沒有讓它工作。表單只是未經驗證並默默提交。可能是我的錯誤?

錯誤消息未打印。

<html> 
    <head> 
     <title></title> 
     <link href="css/program-07.css" rel="stylesheet" type="text/css"/> 
      <div id="requirements" style="margin: 0 auto; text-align:justify; width:850px;"> 
       <h1></h1> 
     </head> 
     <body> 
      <nav class="sidenav"> 
       <header class="w3-container"> 
       <h3>Navigation</h3> 
       </header> 
        <a href="http://lineofcode.com/itse2302-002/itse2302-002-007/">Home</a>  
        <a href="index.php">Program07</a> 
        <a target="_blank" href="selfprocessor.php">Self Processor</a> 
        <a target="_blank" href="validatecontrols.php">Validate Controls</a> 
      </nav> 

      <div id="main" style="margin-left:25%"> 

      <?php 
       echo "<h2>Requirement 01 - Datalists + Navigation</h2>"; 
       echo "<h3>To the left are navigation links to the files in this program, and below (not visible) datalists of the days of the week and months of the year that will be used in requirement 2.";   ?> 


        <datalist id="daysweek"> 
         <option value="Monday"> 
         <option value="Tuesday"> 
         <option value="Wednesday"> 
         <option value="Thursday"> 
         <option value="Friday"> 
         <option value="Saturday"> 
         <option value="Sunday"> 
        </datalist> 

        <datalist id="monthsyear"> 
         <option value="January"> 
         <option value="February"> 
         <option value="March"> 
         <option value="April"> 
         <option value="May"> 
         <option value="June"> 
         <option value="July"> 
         <option value="August"> 
         <option value="September"> 
         <option value="October"> 
         <option value="November"> 
         <option value="December"> 
        </datalist> 

      <?php   
       echo "<hr>"; 

       echo "<h2>Requirement 02 - Datalists</h2>"; 
       echo "<h3>The two datalists from above are now incorporated into a form that the user can see and interact with.</h3>"; 
       echo "Input a day of the week and a month of the year."; 
      ?> 
        <form action="daymonth.php" method="get"> 
         <input list="daysweek" name="days"> 
         <datalist id="day"> 
          <option value="Monday"> 
          <option value="Tuesday"> 
          <option value="Wednesday"> 
          <option value="Thursday"> 
          <option value="Friday"> 
          <option value="Saturday"> 
          <option value="Sunday"> 
         </datalist> 

         <input list="monthsyear" name="months"> 
          <datalist id="month"> 
           <option value="January"> 
           <option value="February"> 
           <option value="March"> 
           <option value="April"> 
           <option value="May"> 
           <option value="June"> 
           <option value="July"> 
           <option value="August"> 
           <option value="September"> 
           <option value="October"> 
           <option value="November"> 
           <option value="December"> 
           </datalist> 
         <input type="submit"> 
        </form> 

      <?php     
       echo "<hr>"; 

       echo "<h2>Requirement 03 - Form Processor</h2>"; 
       echo "<h3>A form processor file named daymonth was created for the form data in requirement 2. A function called test_input checks the data for safety.</h3>"; 

       echo "<hr>"; 

       echo "<h2>Requirement 04 - Favorites Form</h2>"; 
       echo "<h3>This form includes required fields.</h3>"; 

       $nameErr = $movieErr = $foodErr = $seasonErr = ""; 
       $name = $movie = $food = $season = $comment = ""; 

       if ($_SERVER["REQUEST_METHOD"] == "POST") { 
        if (empty($_POST["name"])) { 
        $nameErr = "Please tell us your name!"; 
        } else { 
        $name = test_input($_POST["name"]); 
        } 

        if (empty($_POST["movie"])) { 
        $movieErr = "Please tell us your favorite movie!"; 
        } else { 
        $email = test_input($_POST["movie"]); 
        } 

        if (empty($_POST["food"])) { 
        $foodErr = "Please tell us your favorite food!"; 
        } else { 
        $website = test_input($_POST["food"]); 
        } 

        if (empty($_POST["season"])) { 
        $seasonErr = "Please tell us your favorite season!"; 
        } else { 
        $gender = test_input($_POST["season"]); 
        } 

        if (empty($_POST["comment"])) { 
        $comment = ""; 
        } else { 
        $comment = test_input($_POST["comment"]); 
        } 
       } 

      ?> 
       <p><span class="error">* required field.</span></p> 
       <form method="post" action="favorites.php"> 
        Name: <input type="text" name="name"> 
        <span class="error">* <?php echo $nameErr;?></span> 
        <br><br> 
        Favorite Movie: <input type="text" name="movie"> 
        <span class="error">* <?php echo $movieErr;?></span> 
        <br><br> 
        Favorite Food: <input type="text" name="food"> 
        <span class="error">* <?php echo $foodErr;?></span> 
        <br><br> 
        Favorite Season: 
        <input type="radio" name="season" value="Spring">Spring 
        <input type="radio" name="season" value="Summer">Summer 
        <input type="radio" name="season" value="Fall">Fall 
        <input type="radio" name="season" value="Winter">Winter 
        <span class="error">* <?php echo $seasonErr;?></span> 
        <br><br> 
        Comments:<br /> 
        <textarea name="comment" rows="5" cols="40"></textarea> 
        <br><br> 
        <input type="submit" name="submit" value="Submit"> 
       </form> 

       <?php  
       echo "<hr>"; 

       echo "<h2>Requirement 05 - </h2>"; 
       echo "<h3></h3>"; 


       echo "<hr>"; 

       echo "<h2>Requirement 06 - </h2>"; 
       echo "<h3></h3>"; 


       echo "<hr>"; 

       echo "<h2>Requirement 07 - </h2>"; 
        echo "<h3></h3>"; 


       echo "<hr>"; 

       echo "<h2>Requirement 08 - </h2>"; 
       echo "<h3></h3>"; 

       echo "<hr>"; 

       echo "<h2>Requirement 09 - </h2>"; 
       echo "<h3></h3>"; 


       echo "<hr>"; 


      ?> 
       </div> 
      </div> 
     </body> 
</html> 

而且我favorites.php

<head> 
    <title>Program 07 - Nicola Stewart</title> 
    <link href="css/program-07.css" rel="stylesheet" type="text/css"/> 
     <div id="daymonth" style="margin: 0 auto; text-align:justify; width:850px;"> 
</head> 

<body> 
    <nav class="sidenav" style="width:25%"> 
     <header class="w3-container"> 
     <h3>Navigation</h3> 
     </header> 
     <a href="http://lineofcode.com/itse2302-002/itse2302-002-007/">Home</a>  
     <a href="index.php">Program07</a>   
    </nav> 

    <div id="main" style="margin-left:25%"> 

     <h2>Requirement 04 - Favorites Form</h2> 

     <header class="container"> 
     <h1>Welcome!</h1> 
     </header> 
     <div class="container"> 

     <?php 
      $name = $movie = $food = $season = $comment = ""; 

      if ($_SERVER["REQUEST_METHOD"] == "POST") { 
       $name = test_input($_POST["name"]); 
       $movie = test_input($_POST["movie"]); 
       $food = test_input($_POST["food"]); 
       $season = test_input($_POST["season"]); 
       $comment = test_input($_POST["comment"]); 

      } 

      function test_input($data) { 
       $data = trim($data); 
       $data = stripslashes($data); 
       $data = htmlspecialchars($data); 
       return $data; 
      } 

      echo "<h2>You selected:</h2>"; 
      echo "<h3>"; 
      echo "<br>"; 
      echo $name; 
      echo "<br>"; 
      echo $movie; 
      echo "<br>"; 
      echo $food; 
      echo "<br>"; 
      echo $season; 
      echo "<br>"; 
      echo $comment; 
      echo "</h3>"; 
     ?> 

     </div> 
    </div> 

</body> 
+0

這是對'favorites.php'的代碼? – grim

+0

@grim這是我的index.php。給那個faviourates.php。 –

+0

我已經對我的答案做了一些更新。 – grim

回答

2

您可以發佈您的表單數據favorites.php

<form method="post" action="favorites.php"> ... </form> 

將其更改爲:

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

或移動您的表單驗證到favorites.php

如果您想再次顯示錶單,我建議您將負責表單HTML的代碼自行移動到文件中(即, form.php),然後你可以做兩個index.phpfavorites.php如下:

include 'form.php' 

注意,當您在另一個PHP文件,該變量是共享的,這意味着你可以使用/覆蓋變量。

(你也可以做包括頁眉和網站的頁腳)

編輯(回答評論) 更新test_data()

function test_input($index) { 
    if (isset($_POST[$index])) { // make sure that the index exists in $_POST 
    $data = $_POST[$index]; 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
    } 
    return null; // or any default value 
} 

您可以使用它像這樣:

$season = test_input("season"); 

個人喜歡獲取數據fr OM A POST或GET:

filter_input()

$season = filter_input(INPUT_POST, 'season', FILTER_SANITIZE_SPECIAL_CHARS); 
+0

由於某種原因,嚴重的,現在面臨一個問題當我點擊提交,現在在favorites.php上的錯誤是'注意:未定義的索引:季節在C:\ wamp \ www \ program-07 \ favorites.php在65行「 '這一行是'$ season = test_input($ _ POST [「season」]);' –

+0

在這些變化之後http://pastebin.com/qvffdgY4 –

+0

@sᴜʀᴇsʜᴀᴛᴛᴀ你有一個'input'標籤,它的屬性名稱=「season」'?如果不是,將它添加到你的表單中,你也可以使用[isset](http://php.net/manual/en/function.isset.php)來確保你的'$ _POST'有一個特定的索引(即'if(isset($ _ POST ['season']') – grim