动态错误显示

时间:2012-05-09 06:45:41

标签: php html css

请问有没有办法让我的错误显示动态,只显示每个文本框下显示错误的字段。

当一个字段为空时,我通常使用的所有字段只显示一个错误...

由于

          if (isset($_POST['submit'])) {

    $a = mysql_real_escape_string($_POST      ['a']);

   $b = mysql_real_escape_string($_POST       ['b']);

    $c = mysql_real_escape_string($_POST      ['c']);

    $d = mysql_real_escape_string($_POST      ['d']);

    $e = mysql_real_escape_string($_POST      ['e']);


if (($a == "") ||  ($b == "") || ($c == "") ||        ($d == "") || ($e == "")) {

               echo "<div id=\"content\" >" ;
               echo "<div class=\"error\" >" ;  
               echo "empty"; 
               echo "</div>";
               echo "</div>";
            } else { 

 $query = "INSERT INTO user (a, b, c, d, e)
                   VALUES ($a, $b, $c, $d, $e)";
                   mysql_query($query);          


                  }
             }

             ?>

    Enter Texts <br/>
     <form action="<?php echo $_SERVER        ['PHP_SELF']; ?>" method="post"> 

 A:<input type="text"  name="a" ><br/>
 B:<input type="text"  name="b" ><br/>
 C:<input type="text"  name="c" ><br/>
 D:<input type="text"  name="d" ><br/>
 E:<input type="text"  name="e" ><br/>

                    <input type="submit" name           ="submit" value="Go"/> 
     </form>
谢谢。

3 个答案:

答案 0 :(得分:0)

不是在($a == "") || ($b == "") || ($c == "") || ($d == "") || ($e == "")中一次检查所有变量,而是一次检查一个并设置单个错误变量。使用这些可以显示靠近输入字段的错误消息。

例如:

if ( $a == "" ) { $errorA = true; }

A:<input type="text"  name="a" ><br/>
<?php print ($errorA ? '<span class="error">A is empty.</span><br/>' : ''); ?>

要阻止提交表单,您可以使用以下行:

$error['A'] = $error['B'] = $error['C'] = .... = $error['Z'] = 0;    
if ( $a == "" ) { $error['A'] = 1; }
if ( array_sum($error) > 0 ) {
  // do not submit!
}

A:<input type="text"  name="a" ><br/>
<?php print ($error['A'] ? '<span class="error">A is empty.</span><br/>' : ''); ?>

或者您只需设置一个更广泛的错误变量:

$error = false;
if ( $a == "" ) { $errorA = true; $error = true; }
if ( $b == "" ) { $errorB = true; $error = true; }

if ( !$error ) { //submit form 
}

A:<input type="text"  name="a" ><br/>
<?php print ($errorA ? '<span class="error">A is empty.</span><br/>' : ''); ?>

答案 1 :(得分:0)

不要在顶部显示错误,而是在每个字段中附加以下代码。 如果您的表单和操作代码位于同一页面上,则不会在表单加载时显示错误。

尝试这样的事情:

A:<input type="text"  name="a" ><br />
<?php if( isset($_POST['a']) && trim($_POST['a']) == '' ) { echo 'This field is required'; } ?>

B:<input type="text"  name="b" ><br/>
<?php if( isset($_POST['b']) && trim($_POST['b']) == '' ) { echo 'This field is required'; } ?>

C:<input type="text"  name="c" ><br/>
<?php if( isset($_POST['c']) && trim($_POST['c']) == '' ) { echo 'This field is required'; } ?>

D:<input type="text"  name="d" ><br/>
<?php if( isset($_POST['d']) && trim($_POST['d']) == '' ) { echo 'This field is required'; } ?>

E:<input type="text"  name="e" ><br/>   
<?php if( isset($_POST['a']) && trim($_POST['a']) == '' ) { echo 'This field is required'; } ?>

答案 2 :(得分:0)

这是一种替代方法,在你去的时候检查和构建一个错误数组,然后如果错误数组为空则进行查询。

<?php
//Check form was posted
if($_SERVER['REQUEST_METHOD']=='POST'){
    //Pre build allowed array
    $allowed=array('a','b','c','d','e','submit');
    //Pre build errors array
    $errors=array('A was not set',
                  'B was not set',
                  'C was not set',
                  'D was not set',
                  'E was not set');
    //Create Blank error array    
    $error=array();
    //Loop through the POST
    foreach($_POST as $key=>$value){
        //If key is in allowed array
        if(in_array($key,$allowed)){
            //Check its at good length
            if(strlen(trim($value)) >= 1){
                //Assign variable variable the key and value + escape
                $$key = mysql_real_escape_string(trim($value));
            }else{
                //Assign key/value null
                $$key = null;
                //Assign the error from the errors array to the output error array
                $error[$key] = $errors[array_search($key,$allowed)];
            }
        }else{
            $error=array('Rouge POST key');
        }
    }

    //If all is good do query
    if(empty($error)){
        $query = "INSERT INTO user (a, b, c, d, e)
                   VALUES ($a, $b, $c, $d, $e)";
        mysql_query($query);
    }

}?>

Enter Texts <br/>
<form action="" method="post"> 

 A:<input type="text"  name="a" ><?php echo (isset($error['a'])?$error['a']:null)?><br/>
 B:<input type="text"  name="b" ><?php echo (isset($error['b'])?$error['b']:null)?><br/>
 C:<input type="text"  name="c" ><?php echo (isset($error['c'])?$error['c']:null)?><br/>
 D:<input type="text"  name="d" ><?php echo (isset($error['d'])?$error['d']:null)?><br/>
 E:<input type="text"  name="e" ><?php echo (isset($error['e'])?$error['e']:null)?><br/>
 <input type="submit" name="submit" value="Go"/> 
</form>