1

请问有没有一种方法可以使我的错误显示动态显示,仅在每个文本框下显示有错误的字段。

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

谢谢

          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>

谢谢你。

4

3 回答 3

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/>' : ''); ?>
于 2012-05-09T06:49:47.010 回答
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'; } ?>
于 2012-05-09T06:57:30.457 回答
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>
于 2012-05-09T07:16:14.463 回答