Schmoopy
03-06-2009, 04:01 PM
Hi, I'm using some validation on the server side so that if one of the fields is empty it will come back with an error, and some other things.
When I do this it clears the form, which is really annoying as you have to type things out again. I'll just give you some of the code:
if (empty($_POST['username']) || empty($_POST['password']))
{
$error = "install.php?error=null";
redirect_to($error);
}
elseif (strlen($_POST['username']) <= 5)
{
$error = "install.php?error=ulength";
redirect_to($error);
}
elseif (strlen($_POST['password']) <= 5)
{
$error = "install.php?error=plength";
redirect_to($error);
}
Just some simple validation, then the HTML:
<form action="processinstall.php" method="post">
<label for="username">Username:</label> <input type="text" name="username"/><br />
<label for="password">Password:</label> <input type="password" name="password"/><br />
<input type="submit" name="submit" value="Login"/>
</form>
<?php
if(isset($_GET['error']))
{
echo ($_GET['error'] == "plength") ? "Password must contain at least 6 characters." : "";
echo ($_GET['error'] == "ulength") ? "Username must contain at least 6 characters." : "";
echo ($_GET['error'] == "null") ? "Please enter a username and password." : "";
}
?>
The only was so far I've though of is to have the username come back in the form of a $_GET variable, and then just echo the value in the input box. Is there any other way to do this?
Of course I wouldn't do that with the password as that would be very unsecure. So is the $_GET method the best way, or not?
When I do this it clears the form, which is really annoying as you have to type things out again. I'll just give you some of the code:
if (empty($_POST['username']) || empty($_POST['password']))
{
$error = "install.php?error=null";
redirect_to($error);
}
elseif (strlen($_POST['username']) <= 5)
{
$error = "install.php?error=ulength";
redirect_to($error);
}
elseif (strlen($_POST['password']) <= 5)
{
$error = "install.php?error=plength";
redirect_to($error);
}
Just some simple validation, then the HTML:
<form action="processinstall.php" method="post">
<label for="username">Username:</label> <input type="text" name="username"/><br />
<label for="password">Password:</label> <input type="password" name="password"/><br />
<input type="submit" name="submit" value="Login"/>
</form>
<?php
if(isset($_GET['error']))
{
echo ($_GET['error'] == "plength") ? "Password must contain at least 6 characters." : "";
echo ($_GET['error'] == "ulength") ? "Username must contain at least 6 characters." : "";
echo ($_GET['error'] == "null") ? "Please enter a username and password." : "";
}
?>
The only was so far I've though of is to have the username come back in the form of a $_GET variable, and then just echo the value in the input box. Is there any other way to do this?
Of course I wouldn't do that with the password as that would be very unsecure. So is the $_GET method the best way, or not?