2011-08-17 15 views
0

私はPHPで登録フォームを設計しています。私は私のフォームにいくつかのバリデーションを追加したい。バリデーションは、フィールドの検証が必要です。これは私のコードです。PHPで適切な場所に検証を追加するには?

<?php 
    include('connect.php'); 
    if(isset($_REQUEST['Register'])) 
    { 

     $error='';//initialize $error to blank 
    if(trim($_POST[userid])==''){ 
     $error.="Please enter a username!<br />"; //concatenate the $error Message with a line break 
    } 
    if(trim($_POST[password])==''){ 
     $error.="Please Enter password! <br />";//concatenate more to $error 
    } 
    if(trim($_POST[email])=='') 
    { 
    $error.="Please Enter email address !<br />"; 
    } 
    else 
    { 
     if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { 
     $error="The e-mail you entered was not in the proper format!"; 

     } 
    } 
    if(trim($_POST[regAs])=='NULL') 
    { 
    $error.="Please select Register As !<br />"; 
    } 

    if($error!='') 
    { 
    echo "<span style=color:red>$error</span>"; 
    //Hmmmm no text is in $error so do something else, the page has verified and the email was valid 
    // so uncomment the line below to send the user to your own success page or wherever (swap 
    //yourpage.php with your files location). 
    //echo "script type=\"text/javascript\">window.location=\yourpage.php\"<script>"; 
    } 
    else 
    { 
      $userid=$_REQUEST['userid']; 
     $name=$_REQUEST['name']; 
     $password=$_REQUEST['password']; 
     $repassword=$_REQUEST['repassword']; 
     $email=$_REQUEST['email']; 
     $regAs=$_REQUEST['regAs']; 
     if($password!=$repassword) 
     { 
      echo "<script>alert('both are diferent password')</script>"; 
     } 
     else 
     { 

     mysql_query("insert into login(userid,name,password,email,regAs) values('$userid','$name','$password','$email','$regAs')"); 
     } 
    }     










    } 
?> 
<html> 
<head></head> 
<body> 
<form action="" method="post" name="f"> 
<table align="center"> 
<tr> 
<td>User ID :</td><td> 
<input type="text" name="userid" value=""></td> 
</tr> 
<tr> 
<td>Name :</td><td> 
<input type="text" name="name" value=""></td> 
</tr> 
<tr> 
<td>Password : </td> 
<td><input type="password" name="password" value=""></td> 
</tr> 
<tr> 
<td>Re-Enter Password : </td> 
<td><input type="password" name="repassword" value=""></td> 
</tr> 
<tr> 
<td>Email id :</td><td> 
<input type="text" name="email" value=""></td> 
</tr> 
<tr> 
<td>Register As :</td><td> 
<select id="regAs" name="regAs"> 
<option value="NULL">SELECT</option> 
<option value="Artist">Artist</option> 
<option value="Buyer">Buyer</option> 
</select></td> 
</tr> 
<tr> 
<td></td> 
<td><input type="submit" name="Register" value="Register"</td></tr> 
</table> 
</form> 

</body> 
</html> 

これは検証を確認するための出力画面です。これらのエラーメッセージは最初の行に表示されます。しかし、私はそのエラーが各行の前に来る必要があります。たとえば、ユーザーが電子メールを入力しなかった場合、エラーは電子メールテキストボックスの前に表示されなければなりません。私は何をすべきか教えてください.. ..?

+0

フォームフィールドごとにエラーメッセージ変数を作成し、変数の値を入力フィールドの隣にエコーすることができます。 –

+1

あなたのコードにはさまざまな問題があります:いくつかの配列キーを引用しないで、推奨されないメソッド( 'preg_match'の代わりに' eregi')、** SQL INJECTION **をSQLのクエリにpasswdしないでください。 'error_reporting(E_ALL);'を有効にし、安全なPHPプログラミングについて読んでください! – ThiefMaster

答えて

1

エラーが正しいフォームフィールドの前に表示されるようにするには、HTMLの正しい場所でエラーを取得する必要があります。最も簡単な解決策は、可能なフォームフィールドごとに変数または配列ノードを作成し、必要に応じてエラーメッセージを挿入することです。それはdeprecatadで、さらにユーザーeregiをしません

<tr> 
    <td>Email id :</td> 
    <td> 
    <input type="text" name="email" value=""> 
    <?php echo $error['email']; ?> 
    </td> 
</tr> 

:あなたのHTML内

<?php 
include('connect.php'); 
if(isset($_REQUEST['Register'])) { 
    $errors = array(
     'email' => '', 
     'username' => '', 
     //etc... 
    ); 

    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { 
     $error['email'] = "The e-mail you entered was not in the proper format!"; 
    } 
} 
?> 

、その後:私は簡単な例をあげます。代わりにpreg_matchを使用してください。

+0

hoppa先生ありがとう...ありがとうございました... –

+0

私の問題で私を助けてくださいhttp://stackoverflow.com/questions/7077599/send-email-from-php私はあなたがこれを解決できると思います。 –

0

あなたは、各フィールドを検証するとしてあなたは...

// Might as well define them all so we don't have to use isset() later 
// This isn't necessary, it just makes the echo logic easier 
foreach ($_POST as $k => $v) { 
    $errors[$k] = array(); // No error yet 
} 

if (! valid_email($_POST['email'])) { 
    // Add an error to the array 
    $errors['email'][] = 'The email address is not valid.'; 
} 

をあなたのエラーを持つ配列を作成することができます...そして、あなたのHTMLのエラーをエコー:

<tr> 
    <td>Email id :</td> 
    <td> 
    <input type="text" name="email" value=""> 
    <?php echo implode(', ', $errors['email']); ?> 
    </td> 
</tr> 

私はスプリットにimplode()を使用メッセージはコンマで区切り、あなたのために何でもしてください。私はあなたが各分野のために複数のエラーを収集したいと思うと仮定しました。フィールドごとに1つのエラーだけが必要な場合は、各フィールドに配列ではなく文字列を使用することを検討してください。

関連する問題