Edited by Roger, 05 August 2010 - 02:59 PM.
Validation - How can I validate a form with PHP
Started by John Ugwuozor, Aug 03 2010 04:56 AM
2 replies to this topic
#1
Posted 03 August 2010 - 04:56 AM
How can I validate a form with PHP
|
|
|
#2
Posted 03 August 2010 - 07:24 AM
Mainly, you check each value in the form and returns error messages and shows the form again if the validation failed. How you do it is so different to each form of validation
but an example could be:
but an example could be:
if ($_POST['submit'] != "") {
// this is where we do the validation.
if ($_POST['myfield'] == "") {
$errormessage = "you need to write something";
} else if (strlen($_POST['myfield'] > 30) {
$errormessage = "Please don't write more than 30 characters";
}
if ($errormessage != "" || $_POST['submit'] == "") {
// if there is an error, or, the form isn't submitted yet, print the form
echo '<form method="POST" action="form.php">';
echo '<input type="text" name="myfield" value="'.$_POST['myfield'].'" />';
echo '<input type="submit" name="submit" value="Send in form" />';
echo $errormessage;
} else {
// what to do if the validation passed
echo "you wrote ". $_POST['myfield'];
}
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#3
Guest_johnny.dacu_*
Posted 04 August 2010 - 09:20 AM
Guest_johnny.dacu_*
Orjan has given to you a greate example. With other words you grap a value (if form method is post then use $_POST['var_name'] else if is get then... $_GET. Above example check if the value if empty or it has more than 30 chars. But there are many other options like checking if is a number, or if it has a pattern etc.


Sign In
Create Account

Back to top









