Log in

View Full Version : PHP Form Validation...



LMS16
06-12-2010, 01:35 PM
Hey,

Does anyone know of any good methods of PHP form validation?

Basically all I am after is a way for a user to submit a form, if they left out a required field alert them but keep all data they have already entered?

+REP

Lew.

Dentafrice
09-12-2010, 09:27 PM
Basic.. but you should get the idea.


<?php
if($_POST) {
$name = $_POST["name"]; // clean this of course...

if($name == "") {
$error = "Name left blank..";
} else {
// wasn't blank..
}

}
?>

<?php
if($error != "") {
echo "<h1>ERROR!:</h1> <p>{$error}</p>";
}
?>

<form name="blah" method="post" action="index.php">
<label>Name:</label>
<input type="text" value="<?php echo $name; ?>" />
<input type="submit" />
</form>

triston220
17-12-2010, 03:45 PM
<form action="index.php" method="post">
<input type="text" name="Name">
<input type="submit" name="submit" value="Gooo!">
</form>

<?php
if (isset($_POST['submit'])) { //Has the user pressed the button?
if ($_POST['Name'] =='') { //Is there a value for name?
Echo "Enter a value"; //Not here.
}else{
echo "validation passed!"; //Yay, the user entered a value which was not null.
}
}
?>

LMS16
17-12-2010, 04:49 PM
<form action="index.php" method="post">
<input type="text" name="Name">
<input type="submit" name="submit" value="Gooo!">
</form>

<?php
if (isset($_POST['submit'])) { //Has the user pressed the button?
if ($_POST['Name'] =='') { //Is there a value for name?
Echo "Enter a value"; //Not here.
}else{
echo "validation passed!"; //Yay, the user entered a value which was not null.
}
}
?>

I found a solution in the end + the way you've done it, it wont keep the values in text boxes.

Lew.

Dentafrice
17-12-2010, 08:43 PM
Not trying to be a party pooper... but read the post next time before you try and offer a solution on what he wanted...

Want to hide these adverts? Register an account for free!