Validating form data : Form Validation « Form « PHP






Validating form data

 
<?
if ($_POST['_submit_check']) {
    if (validate_form()) {
        process_form();
    } else {
        show_form();
    }
} else {
    show_form();
}

function process_form() {
    print "Hello, ". $_POST['my_name'];
}

function show_form() {
    print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="my_name">
<br/>
<input type="submit" value="Say Hello">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}

function validate_form() {
    if (strlen($_POST['my_name']) < 3) {
        return false;
    } else {
        return true;
    }
}
?>
  
  








Related examples in the same category

1.Form value validation: not empty
2.Form Data Validation With Error Count
3.A Sample Form Element Validation Function
4.Displaying error messages with the form
5.Elementary Form Validation
6.Form Example for the Form Validator Script