首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP表单-错误消息放置

PHP表单-错误消息放置
EN

Stack Overflow用户
提问于 2011-06-27 18:11:09
回答 2查看 433关注 0票数 0

我已经创建了一个具有多个字段的表单,我将这些字段发布到数据库中。

在表单的底部,我设置了一些简单的错误检查,以确保所有必填字段都是完整的。

当用户单击submit按钮时,我的错误检查就会发生,如果有错误,它将输出一个字符串,其中包含它遇到的错误。

这些错误当前显示在表单的下方,但我希望它们显示在表单域的上方。有没有什么办法可以让我这样做?

你知道我该怎么做吗?

以下是代码

代码语言:javascript
复制
enter code here
<! Code to check that the user has logged into to view this page !>
<?php
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login.php");
}
?>
<!Connection details for connecting to mysql database!>


<!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>';
}


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

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

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


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

$studentNameError = "You did not enter the student name";

$error = false;
if(empty($_POST['studentName'])) 
{
$studentNameError = "You did not enter the student name";
echo "<h3> $studentNameError </h3>";
$error = true;
}
//Code to check that the Tutor Name field is completed
if(empty($_POST['tutorName'] ))
{
echo "<h3>You did not select a tutor name.</h3>";
$error = true;
}
//Code to check that the Procedure field is completed
if(empty($_POST['procedure'] ))
{
echo  "<h3>You did not select a procedure.k</h3>";
$error = true;
}
//Code to check that the Grade field is completed
if(empty($_POST['grade'] ))
{
echo "<h3>You did not select a grade.</h3>";
$error = true;
}
//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. </h3>";
$error = true;
}
//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>";
    $error = true;
}
if($error)
{
    exit();
}

  ?>
<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="form.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 if(isset($_POST['studentName'])) echo $_POST['studentName'];?>
        <?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 if(isset($_POST['tutorName'])) echo $_POST['tutorName'];?>
      <?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>
        <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="submit" name="mattbutton" class="mattbutton" value="Update Database"  name="submit"/> 
  <input type='button' name="mattbutton" class="mattbutton" value='Logout' onClick="window.location.href='logout.php'">  
  <input type="hidden" name="submited" value="true" />
  <p align="left">
    <?php

//Code to turn off error reporting
//error_reporting(0);

//Error Message to display if all the correct fields are not completed.



//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 : You are attempting to enter information which cannot be stored or contains code. Please refesh the from and try again<br>' .mysql_error());


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

回答 2

Stack Overflow用户

发布于 2011-06-27 18:15:30

将错误检查代码放在窗体呈现代码的“上方”应该不会有任何问题。一旦表单提交(大概是通过POST ),您就拥有了一个数组( $_POST数组)中的所有表单变量,无论您是否决定重新呈现表单,都可以使用这些变量。

票数 0
EN

Stack Overflow用户

发布于 2011-06-27 18:18:58

您可以在表单中创建一个隐藏字段,例如

代码语言:javascript
复制
<input type="hidden" name="isSubmitted" value ="1">

然后,在您的检查例程中,您首先检查$_POST'isSubmitted' == 1是否为真,然后您就知道用户已经提交了您的表单,然后您可以进行其他检查

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6491350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档