我創建了一個簡單的表單,其中包含一些必填字段,如果未完成,則會向用戶傳回錯誤,以通知他們該字段是必需的。 由於有多個字段是檢查它可以輸出多個錯誤消息。Php表單 - 錯誤檢查和輸出
我想知道如何在我的表單上定義一個區域,這些錯誤顯示在屏幕上,這些錯誤只顯示在窗體的底部,除非您向下滾動頁面,否則無法看到這些錯誤。
我可以定義我的錯誤顯示位置嗎?
這裏是錯誤校驗碼:編輯
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();
}
}
請向我們提供完整的HTML和PHP這種形式,我可以幫忙。只要給出部分的php來做錯誤檢查並不能提供回答這個問題所需的所有信息。 – James
如果使用適當的縮進,您會發現開發和調試代碼更容易。 –
我會建議使用某種形式的JavaScript來直接向用戶提供前端錯誤檢查。這隻會使用php而變得非常混亂。 –