我创建了一个简单的表单,其中包含一些必填字段,如果没有完成,则会将错误传回用户以通知他们该字段是必需的。 由于存在多个检查字段,因此可以输出多个错误消息。
我想知道如何在我的表单上定义一个显示这些错误的区域,因为这些错误只显示在表单的底部,除非您向下滚动页面,否则您无法看到。
我可以定义错误的显示位置。
以下是错误检查代码:EDIT
Old code was here
过去的人建议我做一个循环来一次检查一个错误,但我是php的新手,所以不知道怎么做。
$errors = '';
if(empty($_POST['studentName']))
{
$errors .= "You did not enter the student name<br/>";
}
//Code to check that the Tutor Name field is completed
if(empty($_POST['tutorName'] ))
{
$errors .="You did not select a tutor<br/>";
}
//Code to check that the Procedure field is completed
if(empty($_POST['procedure'] ))
{
$errors .="You did not enter a procedure<br/>";
}
//Code to check that the Grade field is completed
if(empty($_POST['grade'] ))
{
$errors .="You did not enter a grade<br/>";
}
//Code to check that the Student Reflection field is completed
if(empty($_POST['studentReflection'] ))
{
$errors .="You did not enter a reflection<br/>";
}
//Code to check if the tick box is checked that the tutor comment is entered
if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] ))
{
$errors .="You must enter a reasan why you ticked the alert box";
}
//Code to check the password field is completed and correct
if (empty($_POST['password']))
{
$errors .="You did not enter you password";
}
if (!empty($_POST['password']))
{
//==========================================
// ESCAPE DANGEROUS SQL CHARACTERS
//==========================================
function quote_smart($value, $handle) {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value, $handle) . "'";
}
return $value;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$masterpass = $_POST['password'];
$masterpass = htmlspecialchars($masterpass);
//==========================================
// CONNECT TO THE LOCAL DATABASE
//==========================================
$user_name = "username";
$pass_word = "password";
$database = "name of database";
$server = "server";
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$masterpass = quote_smart($masterpass, $db_handle);
$SQL = "SELECT * FROM masterpass WHERE password = $masterpass";
$result = mysql_query($SQL);
$num_rows = mysql_num_rows($result);
//====================================================
// CHECK TO SEE IF THE $result VARIABLE IS TRUE
//====================================================
if ($result) {
if ($num_rows > 0) {
echo "";
}
else {
$errors .= "Password was not recognised";
exit();
}
}
mysql_close($db_handle);
}
}
if(!empty($errors))
{
echo '<div class="errors">' . $errors . '</div>';
exit();
}
}
答案 0 :(得分:2)
您可以执行类似
的操作$errors = '';
if(empty($_POST['studentName']))
{
$errors .= "You did not enter the student name<br />";
}
if(empty($_POST['tutorName'] ))
{
$errors .= "You did not select a tutor name.<br />";
}
// etc.
然后高于<form>
if (!empty($errors))
{
echo '<div class="errors">' . $errors . '</div>';
}
使用CSS设计样式.errors
,因此它会更突出。如果应用程序逻辑中的$errors
为空白,则可以执行对数据库的常规添加/更新并重定向到 success 页面。
答案 1 :(得分:0)
将错误定义为数组也有效,并允许您对错误过程进行更细粒度的控制。
$errors = array();
if(empty($_POST['studentName']))
$errors[] = "You did not enter the student name";
if(empty($_POST['tutorName'] ))
$errors[] = "You did not select a tutor name.";
//etc...
//At the top of your page.
if (sizeof($errors)>0) {
echo "<div class='errordiv'>";
echo "<ul>";
foreach ($errors as $err) {
echo "<li>".$err."</li>"; //or whatever format you want!
}
echo "</ul></div>";
}
您还可以将错误数组作为参数传递给其他函数,记录它们等等。