0

Here is the html and Php code i have used:

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

And these are functions I have used

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;

}

Can someone please guide me to validate the form using if array key exists So that I get error message next to Or below the input field or any other method which does the needful

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

4 Comments

@evgenijs Vaikulis. I guess he wanted to print the error message next to or below the input fields separately. And you have done right but you have printed the error message all at once on the top.
yes want print below the input field ,how can i do that?
ive changed script a bit. Errors must be shown right after your form.
I am still getting the errors above the form,and i have asked for array exists key so that i can find out the key and print my error wherever i want
0

@sandeep S K Can you please try once below code? :

Note: Please include your required DB connection or validation files on the top of the script.

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

1 Comment

want to know more about PHP weblineindia, how can i?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.