Log in

View Full Version : Simple question about forms



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?

JasonDFR
03-06-2009, 08:01 PM
I can give you a better example if needed. Check out this post:

http://www.dynamicdrive.com/forums/showthread.php?t=42281

Use one file to handle and display the form. This will leave the $_POSTed values available to fill in the form values if errors have occurred.

J

Schmoopy
03-06-2009, 08:23 PM
Ah that's just what I need, thanks :)

Schmoopy
03-06-2009, 09:27 PM
One more thing though, where can I find a list for what all the preg_match / ereg patterns do. For example "^[a-zA-Z]+$", where can I see others like these that determine what can go through and what can't?

Thanks.

JasonDFR
03-07-2009, 04:57 PM
http://www.google.com/cse?cx=002683415331144861350%3Atsq8didf9x0&q=regex&ie=utf-8&oe=utf-8&cof=FORID%3A1&sa=Search

Try these links, they seem pretty good.

There really isn't a "list", although there are some collections of commonly used regular expressions.

Do not use the ereg (POSIX), use Perl Compatibile. I read somewhere that ereg is going to be deprecated soon and removed in PHP 6, but I can't remember where.

This is the link you want to look at:

http://www.php.net/manual/en/book.pcre.php

JasonDFR
03-07-2009, 04:59 PM
Found some info on ereg being dropped:

http://www.google.com/cse?cx=002683415331144861350%3Atsq8didf9x0&q=POSIX+deprecated+php&ie=utf-8&oe=utf-8&cof=FORID%3A1&sa=Search