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


Form validation using if array key exists

这是我使用的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;
}

如果数组键存在,是否有人可以指导我使用验证表单这样我就可以在输入字段的"或"旁边得到错误消息或执行必要的任何其他方法

<?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>

@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>