使用if数组键进行表单验证

时间:2014-10-11 06:08:50

标签: php

这是我使用过的html和Php代码:

<?php 
 require_once('include_function.php');
 require_once('validation_functions.php');
 $errors = array();
 $message ="";
 if(isset($_POST['submit'])){
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    if (array_key_exists('submit', $_POST)){
    }
    $fields_required = array("username","password");
    foreach($fields_required as $field){
        $value = trim($_POST[$field]);
        if(!has_presence($value)){
            $errors[$field]= ucfirst($field). " Cant Be blank";
        }
    }
 }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
 <title>Single page Submission</title>
</head>
<body>
    <?php echo $message;?>
    <?php echo form_error($errors)?>
    <form action="form_with_validation.php" method="post">
        Username<input type="text" name="username" value=""/><br/>
        Password<input type="password" value="" name="password" /><br/>
        <input type="submit" name="submit" value="Submit" />
    </form>
</body>
</html>

这些是我用过的功能

function has_presence($value){
    return isset($value) && $value !=="";
}
function form_error($errors=array()){
    $output = "";
    if(!empty($errors)){
        $output .="<div class=\"error\">";
        $output .="Please fix the following errors";
        $output .="<ul>";
        foreach($errors as $key => $field){
            $output .= "<li>{$field}</li>";
        }
        $output .="</ul>";
        $output .="</div>";
    }
    return $output;

}

有人可以指导我使用验证表单,如果存在数组键 这样我就会在输入字段下方或下方收到错误消息 或任何其他需要的方法

2 个答案:

答案 0 :(得分:1)

<?php 
    $errors = array();
    $message ="";
    if(isset($_POST['submited'])){
        $fields_required = array("username","password");
        foreach($fields_required as $field)
            if (!isset($_POST[$field]) || $_POST[$field]=="")
                $errors[$field]= $field. " Cant Be blank";
    }

    function form_error($errors=array()){ 
        $output = "";
        if(!empty($errors)) {
            $output .="<div class=\"error\">";
            $output .="Please fix the following errors";
            $output .="<ul>";
            foreach($errors as $key => $field){
                $output .= "<li>{$field}</li>";
            }
            $output .="</ul>";
            $output .="</div>";
        }
        return $output;
    }
}  
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Single page Submission</title>
</head>

<body>
<?php echo $message;?>
<form action="form_with_validation.php" method="post">
<input type="hidden" name="submited" value="1" />
Username<input type="text" name="username" value=""/><br/>
Password<input type="password" value="" name="password" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php echo form_error($errors)?>
</body>
</html>

答案 1 :(得分:0)

@sandeep S K你能在代码下面试一次吗? :

注意:请在脚本顶部包含所需的数据库连接或验证文件。

 <?php 
    function has_presence($value){
        return isset($value) && $value !=="";
    }
    function form_error($errors=array()){
        $output = "";
        if(!empty($errors)){
            $output .="<div class=\"error\">";
            $output .="Please fix the following errors";
            $output .="<ul>";
            foreach($errors as $key => $field){
                $output .= "<li>{$field}</li>";
            }
            $output .="</ul>";
            $output .="</div>";
        }
        return $output;

    }

     $errors = array();
     $message ="";
     if(isset($_POST['submit'])){
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        if (array_key_exists('submit', $_POST)){
        }
        $fields_required = array("username","password");
        foreach($fields_required as $field){
            $value = trim($_POST[$field]);
            if(!has_presence($value)){
                $errors[$field]= ucfirst($field). " Cant Be blank";
            }
        }
     }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
     <title>Single page Submission</title>
    </head>
    <body>
        <?php echo $message;?>
        <?php  if(!empty($errors)) { ?>

                <div class="has_errors"> 
                    <ul>
                        <li>    
                            There are errors in your input. Please fix them and try again.
                        </li>
                    </ul>
                </div>
            <?php
        }
        ?>
        <form action="index.php" method="post">
            Username<input type="text" name="username" value=""/>
            <?php 
                if (array_key_exists('username', $errors)) {
                    ?>
                    <div class="has_errors"> 
                        <ul>
                            <li>    
                                <?php print $errors['username'];?>
                            </li>
                        </ul>
                    </div>
                    <?php
                }
            ?>
            <br/>
            Password<input type="password" value="" name="password" />
             <?php 
                if (array_key_exists('password', $errors)) { ?>
                    <div class="has_errors"> 
                        <ul>
                            <li>    
                                <?php print $errors['password'];?>
                            </li>
                        </ul>
                    </div>
                    <?php
                }
            ?>
            <br/>
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
    </html>