SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Validating form

  1. #1
    SitePoint Zealot daveporter's Avatar
    Join Date
    Mar 2002
    Location
    Perth, Western Australia
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating form

    Hi All,

    I am learning to validate a simple form with a single entry.

    I am using $_GET in the input and using PHP_SELF to test for form being submitted, then displaying an error if the user enters invalid information.

    It works find uploaded to the web, but fails locally.

    I'm using WIndows XP and PHP 4.3.3

    Hope someone can help.

    TIA - Dave Porter

    PHP Code:
    <?php # Script 4.1 simple version djp
    if (isset($_POST['submit'])) { // If the form was submitted, process it.
     // Check the username.
     
    if (eregi ("^[[:alnum:]]+$"$_POST['username'])) {
      
    $a TRUE;
     } else {
      
    $a FALSE;
      
    $message[] = "Please enter a username that consists only of letters and numbers.";
     }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "[url=http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd]http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd[/url]">
    <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]">
    <head>
     <title>Register</title>
    </head>
    <body>
    <?php
    // Print out any error messages.
    if ($message) {
     echo 
    "<div align=\"left\"><font color=red><b>The following problems occurred:</b><br />\n";
     foreach (
    $message as $key => $value) {
      echo 
    "$value <br />\n";
     }
    }
    ?>
    <?php 
    # daves test
     
    echo "<p>test</p>";
     if (isset(
    $_POST['submit'])) {
        echo 
    "<p>submit 1</p>";
        } else {
        echo 
    "<p>submit 2</p>";
        }
        echo (
    '<p>test ' $_POST['username'] . '</p>');
    ?>
    <!-- hello -->
    <!-- <form action="regsimp.php" method="post"> -->
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table border="0" width="90%" cellspacing="2" cellpadding="2" align="center">
     <tr>
      <td align="right">Username</td>
      <td align="left"><input type="text" name="username" value="<?php echo $_GET['username']; ?>" size="25" maxsize="16" </td>
      <td align="left"><small>Maximum of 16 characters, stick to letters and numbers, no spaces, underscores, hyphens, etc.</small></td>
     </tr>
     <tr>
      <td align="center" colspan="3"><input type="submit" name="submit" value="submit"> &nbsp; &nbsp; &nbsp; <input type="reset" name="Reset" value="Reset"></td>
     </tr>
    </table>
    </form>
    </body>
    </html>
    Last edited by daveporter; Oct 16, 2003 at 07:04.

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is the script with the problem ? I would be a tremendous benifit if you post the script yes ?

  3. #3
    SitePoint Zealot daveporter's Avatar
    Join Date
    Mar 2002
    Location
    Perth, Western Australia
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Done - added to original message !

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    value="<?php echo $_GET['username']; ?>"
    Why'd you be using $_GET when it is in fact $_POST[] you're using ?

    ie This...

    PHP Code:
    ... method="post" ... 
    Going by your FORM, $_GET is empty() yes ?

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At the very start of your script try using

    PHP Code:
    echo('<pre>');
    print_r($_GET[]);
    echo(
    '</pre>'); 
    Likewise on $_POST just to see exactly where your INPUTs are yes ?

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    There's a small error in D.Livingstons code. Probably a type .
    You can't have the [] after the $_GET. That will give a fatal error.
    That said; using print_r is one of the best debugging methods

    -Helge

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your right of course; Sorry folks, never noticed I'd typed the brackets in there

    Oooppss..

  8. #8
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And just as an addition...doing
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    .....
    would have highlighted the mixup between using $_GET and $_POST with an 'Undefined Index' error/warning

  9. #9
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Couldn't agree more markl999. Using E_ALL will save you a lot of error checking when you develop.

    -Helge


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
  •