Results 1 to 6 of 6

Thread: Simple question about forms

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default Simple question about forms

    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:

    PHP 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:

    Code:
    			<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?

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I can give you a better example if needed. Check out this post:

    http://www.dynamicdrive.com/forums/s...ad.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

  3. The Following User Says Thank You to JasonDFR For This Useful Post:

    Schmoopy (03-06-2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ah that's just what I need, thanks

  5. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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.

  6. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    http://www.google.com/cse?cx=0026834...%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

  7. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •