私は、フィールドが必須であることを知らせるために、完了していなければユーザーにエラーを返すいくつかの必須フィールドを持つ簡単なフォームを作成しました。 複数のフィールドがチェックされているので、複数のエラーメッセージを出力できます。phpフォーム - エラーチェックと出力
これらのエラーがフォームの一番下に表示される瞬間に表示されるフォームの領域を定義する方法を知りたいのですが、ページをスクロールしない限り表示されません。
エラーの表示場所を定義できますか。 EDIT過去に
Old code was here
人々は、私は時間に1つずつエラーをチェックするためにループを作る示唆しているが、私は、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();
}
}
など、それらのログを、他の関数へのパラメータとして周りのエラー配列を渡すことができます。エラーチェックを行うPHPのセクションを与えるだけでは、この質問に答えるために必要なすべての情報が提供されるわけではありません。 – James
適切なインデントを使用すると、コードの開発とデバッグが容易になります。 –
私はフロントエンドエラーチェックのためのjavascriptの何らかの形式をユーザに直接使用することをお勧めします。これはちょうどPHPを使用すると非常に乱雑になることができます。 –