验证PHP表单字段

验证PHP表单字段,php,webforms,Php,Webforms,我已经建立了一个表格,上面有一个勾选框,还有一些文本区域可以输入评论。如果用户勾选了tickboxalert,我希望用户必须完成textareatutorComment 编辑:这是我的完整代码 if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] )){ echo "<h3>You must enter a reason why you have clicked the alert box</

我已经建立了一个表格,上面有一个勾选框,还有一些文本区域可以输入评论。如果用户勾选了tickboxalert,我希望用户必须完成textareatutorComment

编辑:这是我的完整代码

if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] )){
echo "<h3>You must enter a reason why you have clicked the alert box</h3>";
exit();}
我能不能不使用isset两次?我有点搞不懂为什么代码停止工作了

提前谢谢

这是完整的代码

    <! Code to check that the user has logged into to view this page !>

<!Connection details for connecting to mysql database!>
<?php


//Select which database you want to connect to



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Op Tech Database - Add Record</title>
</head>
<!Code to Create drop down menu's!>
<?php
//Code for collectiing values for Student Names drop down drop
$result1=mysql_query("SELECT studentID, studentName FROM students");
$options1="";
while ($row=mysql_fetch_array($result1)) {

    $id=$row["studentID"];
    $first=$row["studentName"];

    $options1.="<OPTION VALUE=\"$first\">".$first.'</option>';
    }

//Code for getting tutors names in drop down list
$result2=mysql_query("SELECT staffID, tutorName FROM staff");

$options2="";
while ($row=mysql_fetch_array($result2)) {

    $id=$row["staffID"];
    $first=$row["tutorName"];

    $options2.="<OPTION VALUE=\"$first\">".$first.'</option>';
}
  ?>
<body>
<link rel="stylesheet" type="text/css" href="ex1.css" >
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<!Create HTML elements!>
<form name="myform" form method="post">
  <h1 align="center"><a href="both.php"><img src="colour_logo_400.jpg" alt="University Logo" width="400" height="185" /></a></h1>
  <h1 align="center">Dental Hygiene Operative Technique Database</h1>
  <h2 align="center">Welcome to the Dental Hygiene Operative Technique Database v1</h2>
  <p align="left">&nbsp;</p>
  <p align="left">Student Name</p>
  <p align="left">

    <! Drop Down Menu to get student names from database !>
    <SELECT NAME=studentName >
        <OPTION VALUE=0 selected="selected">
            <?php echo $options1?>
    </SELECT>  

  <p align="left">Tutor Name  
  <p align="left"> 
    <! Drop Down Menu to get tutor names from database !>
    <select name=tutorName>
        <option value=0>
            <?php echo $options2 ?> </option> 
    </select>
  <p align="left">
  <p align="left"><br>


    Procedure
    <input type="text" name="procedure"  value="<?php if(isset($_POST['procedure'])) echo $_POST['procedure'];?>" />
    <select name=grade id=grade>
      <option value="">Grade </option>
      <option value="N" <?php if (isset($_POST['grade']) && $_POST['grade'] == "N") { echo 'selected="selected"';} ?>>N</option>
      <option value="B" <?php if (isset($_POST['grade']) && $_POST['grade'] == "B") { echo 'selected="selected"';} ?>>B</option>
      <option value="C" <?php if (isset($_POST['grade']) && $_POST['grade'] == "C") {   echo 'selected="selected"';} ?>>C</option>
    </select>
    <p align="left">

 Student Reflection:
        <br>
        <textarea name="studentReflection" cols="75" rows="5"><?php if(isset($_POST['studentReflection'])) echo $_POST[             'studentReflection'];?></textarea>
  <p align="left">
        <SELECT NAME=professionalism>
          <OPTION VALUE="">Professionalism
          <OPTION VALUE="U" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "U") {
      echo 'selected="selected"';} ?>>U</option>
          <OPTION VALUE="S" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "S") {
      echo 'selected="selected"';} ?>>S</option>
          <OPTION VALUE="E" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "E") {
      echo 'selected="selected"';} ?>>U</option>
        </SELECT>
        </SELECT>

      <SELECT NAME=communication>
        <OPTION VALUE="">Communication
        <OPTION VALUE="U" <?php if (isset($_POST['communication']) && $_POST['communication'] == "U") {
      echo 'selected="selected"';} ?>>U</option>
        <OPTION VALUE="S" <?php if (isset($_POST['communication']) && $_POST['communication'] == "S") {
      echo 'selected="selected"';} ?>>S</option>
        <OPTION VALUE="E" <?php if (isset($_POST['communication']) && $_POST['communication'] == "E") {
      echo 'selected="selected"';} ?>>U</option>
    </SELECT>

        Alert:
        <input type="checkbox" value="YES" name="alert" >  
        <br>
        <br>

        Tutor Comments:<br>
      <textarea name="tutorComments" cols="75" rows="5"><?php if(isset($_POST['tutorComments'])) echo $_POST['tutorComments'];?>            
        </textarea>
  <p align="left">


<!Submit buttons for the form!>
  <input type="hidden" name="submited" value="true" />
  <input type="submit" value="Update Database"  name="submit"/> 
  <input type='button' value='Logout' onClick="window.location.href='http://address/php_sandbox/optech/dh/current/14june/logout.php'">  
  <p align="left">
    <?php
//Error Message to display if all the correct fields are not completed.


if(isset($_REQUEST['submited'])) {

$errorMessage = "This is the standard error message";

$options1 = $_POST['studentName'];
$options2 = $_POST['tutorName'];
$procedure = $_POST['procedure'];
$grade = $_POST['grade'];
$studentReflection = $_POST['studentReflection'];
$professionalism = $_POST['professionalism'];
$communication = $_POST['communication'];
$tutorComments = $_POST ['tutorComments'];


if(empty($_POST['alert']))
{
$_POST['alert'] = "NO";
}
$alert = $_POST['alert'] ;


//Code to check that the Student Name field is completed
if(empty($_POST['studentName'])) 
{
echo "<h3>You have not selected a student. Please go back and do so!</h3>";
exit();
}
//Code to check that the Tutor Name field is completed
if(empty($_POST['tutorName'] ))
{
echo "<h3>You did not select a tutor name. Please go back and select your name from the tutors list</h3>";
exit();
}
//Code to check that the Procedure field is completed
if(empty($_POST['procedure'] ))
{
echo  "<h3>You did not select a procedure. Please go back and enter the name of the procedure which you undertook</h3>";
exit();
}
//Code to check that the Grade field is completed
if(empty($_POST['grade'] ))
{
echo "<h3>You did not select a grade. Please go back and select your grade from the drop down list</h3>";
exit();
}
//Code to check that the Student Reflection field is completed
if(empty($_POST['studentReflection'] ))
{
echo "<h3>The student did not enter any comments for this procedure. Student reflection is required for each procedure. Please go back and enter any comments</h3>";
exit();
}
//Code to check if the tick box is checked that the tutor comment is entered

if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] )){
    echo "<h3>You must enter a reason why you have clicked the alert box</h3>";
    exit();
}



//Code to connect to the database




$query= "INSERT INTO entry (entryID, studentName , tutorName , procedureName , grade , studentReflection , tutorComments,  professionalism , communication , alert  ) VALUES ('NULL', '".$options1."' , '".$options2." ' , '".$procedure."' , '".$grade."' , '".$studentReflection."', '".$tutorComments."' , '".$professionalism."' , '".$communication."' , '".$alert."'  )";


mysql_query($query) or die ('Error : Something fucked up' .mysql_error());


echo "<h3>The Database Has been updated. Thanks </h3></b>" ; 
}
?>
</FORM>
<p>  <a href="both.php">Enter another procedure </a>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

这是很多代码,我不能说具体是什么,但有一些可能的问题需要考虑:

看起来您在顶部,在DOCTYPE等之前的行中缺少了一个“?>”

表单没有操作=因此请输入当前php文件或$\u服务器的名称['php\u SELF']

学生姓名选项和警报输入没有结束标记

尝试在submit按钮上执行isset,无需隐藏输入:

ifisset$_请求['submit']{或ifisset$_POST['submit']{

如果它仍然不工作,请尝试在输出表单之前将整个代码块移到顶部

不要为此使用empty,只需检查它是否已设置:

if(isset($_POST['alert']))
    $alert = $_POST['alert'];
else 
    $alert = "";
或者简而言之:$alert=isset$_POST['alert']?$_POST['alert']:

与此相反:

if( !strlen($_POST['tutorComments']) 
  && isset($_POST['alert'] )){
 echo "You must enter a reason why you have clicked the alert box";
 exit();
}
这样写:

if(isset($_POST['alert'])){
    if(!strlen($_POST['tutorComments']))
    echo "You must enter a reason why you have clicked the alert box";
    // leave out the exit()
}

或者尝试不同的测试,例如:ifstrlen$\u POST['tutorComments']<1等。

您是否有一个名为submited的表单字段?现在会发生什么?您是否收到任何错误消息确保您已将PHP配置为在开发时显示错误消息。我有一个名为submited的隐藏按钮?表单现在接受已勾选警告框的提交,并且在它应该显示的tutorComment文本区域中没有任何内容在我添加ifisset$\u请求['submited']之前,我不会这样做,也没有这样做codeA hidden button?如果它是一个隐藏的输入,你需要给它一个值。如果它是一个按钮,它将不会被设置,除非它被点击。它确实有一个值,这不是问题所在。这只是一种方法,让php在点击submit按钮后循环使用我的条件If语句。所有其他条件都按它们的方式工作只需检查表单特定元素的$_POST是否为空,但如果单击勾选框并且导师注释中没有文本,则最后一个将进行计算的POST是否因某些原因不起作用。我们可以查看更多代码吗?如果可以,请查看整个表单,或者只保留起作用的位。您好。可能存在一些语法错误因为我删除了一些连接数据库的代码。我会给你一个建议,然后回去报告。嗨,伙计们。原来我的原始代码确实有效,但不知怎的,tutorComments字段被一些不可见的东西填充了,所以当我认为该字段中没有任何内容时,实际上出现了问题。所以谢谢你的建议。如果有帮助,请点击我答案旁边的“接受”链接?
if(isset($_POST['alert'])){
    if(!strlen($_POST['tutorComments']))
    echo "You must enter a reason why you have clicked the alert box";
    // leave out the exit()
}