错误消息显示在php中

时间:2015-04-20 07:35:16

标签: php forms validation

我想验证表单,但是当我单击“保存”按钮时,它不会显示错误消息。我在if(empty($_POST("lecturer")))中也收到错误,该错误表示无法返回函数值。

表格   

<b>Lecturers:<b/>
<select id="SelectLecturerName" name="lecturer"> 
<option value="Choose">Please select..</option>
    <?php
        $sql = mysql_query("SELECT lec_name FROM lecturer");
        while ($row = mysql_fetch_array($sql)) {
            echo "<option value='" . $row['lec_name'] . "'>" . $row['lec_name'] ."</option>";
        }
    ?>
</select><br/><br/>


A1: <input type="text" name="a1" size="5"> <br /><br/>
A2: <input type="text" name="a2" size="5"> <br /><br/>
A3: <input type="text" name="a3" size="5"> <br /><br/>
L1: <input type="text" name="l1" size="5"> <br /><br/>
L2: <input type="text" name="l2" size="5"> <br /><br/>
L3: <input type="text" name="l3" size="5"> <br /><br/>
L4: <input type="text" name="l4" size="5"> <br /><br/>
L5: <input type="text" name="l5" size="5"> <br /><br/>
L6: <input type="text" name="l6" size="5"> <br /><br/>
L7: <input type="text" name="l7" size="5"> <br /><br/>
LEC AVG: <input type="text" name="lavg" size="5"> <br /><br/>
R1: <input type="text" name="r1" size="5"> <br /><br/>
R2: <input type="text" name="r2" size="5"> <br /><br/>
U1: <input type="text" name="u1" size="5"> <br /><br/>
U2: <input type="text" name="u2" size="5"> <br /><br/>
U3: <input type="text" name="u3" size="5"> <br /><br/>

<b>Year:<b/>
<select name="year"> 
<option value="Choose">Please select..</option>
<option value="2005">2005</option> 
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option></select><br/><br/>

 <b>Unit Name:<b/>
<select id="SelectUnitName" name="unit_name">
<option value="Choose">Please select..</option>

    <?php
        $sql = mysql_query("SELECT unit_name FROM unit");
        while ($row = mysql_fetch_array($sql)) {
            echo "<option value='" . $row['unit_name'] . "'>" . $row['unit_name'] ."</option>";
        }
    ?>
</select>
</form><br /><br/>

<br/>
<input type="submit" name="Submit"value="Save" >
<input type="reset" name="reset" value="Clear">

</form>


    <?php

         $lecNameError="";
         $yearError="";
         $unitError="";

      //check if you didn't choose lecturer name 
       if(isset($_POST['submit'])){

          if(empty($_POST("lecturer"))){

              //error mesg choose a lecturer name
              $lecNameError="Please choose a lecturer name.";

           }
           if (empty($_POST("year"))){

               //error mesg choose a year
                $yearError="Please choose a year.";
           }

           if(empty($_POST("unit_name"))) 

               //error mesg choose a unit name
               $unitError="Please choose a unit.";


      }

     ?>

1 个答案:

答案 0 :(得分:2)

您错误地为$ _POST输入括号,因为$ _POST是数组,您无法使用圆括号访问其值。理解php中的数组。

更改您的代码

$_POST("lecturer")

$_POST["lecturer"]

查看此链接以获取更多$ _POST信息 http://php.net/manual/en/reserved.variables.post.php