Form Validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerim79

    #1

    Form Validation

    Here is my form:

    <?php
    $Number;
    $Email;
    $error=0;

    if (empty($Number) ){
    $msg1="* You did not enter the number of people in your
    household.";
    $error++;
    }

    if (empty($Email)) {
    $msg2="* You did not enter your email address.";
    $error++;
    }

    if ($error>0){
    echo "<form action=\"{$_SER VER['PHP_SELF']}\" method=\"POST\" >
    <table width=\"700\" border=\"0\" cellpadding=\"0 \" align=\"center\ ">
    <tr>
    <td colspan=\"34\"> <div align=\"center\ "><strong>SURVE Y
    </strong></div></td>
    </tr>
    <tr>
    <td colspan=\"34\" bordercolor=\"# ECE9D8\" height=\"5\" align=\"center
    \" <hr/<br/ <
    </td>
    </tr>
    <tr>
    <td colspan=\"34\"> How many people are in your household?
    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
    <input type=\"text\" name=\"Number\" width=\"40\" />$msg1</td>
    </tr>
    <tr>
    <td colspan=\"34\" align=\"center\ "><br/></td></tr>
    <tr>
    <td align=\"right\" colspan=\"15\"> Please Enter Your Email
    Address:&nbsp;</td>"
    <td width=\"480\" colspan=\"24\"> <input type=\"text\" name=\"Email\"
    value=\"\" width=\"200\">$ msg2</td>
    </tr>
    <tr>
    <td colspan=\"34\"> &nbsp;</td>
    </tr>
    <tr>
    <td colspan=\"34\" align=\"center\ "><input name=\"Submit\" type=
    \"SUBMIT\" value=\"Continu e\"/></td>
    </tr>
    </table>
    </form>";
    }

    else {
    include('nextpa ge.php');
    }
    ?>

    This works fine in that it catches what it needs to catch. However,
    obviously, it prints out the error message upon first loading the
    page. I would like to wait until after the user has clicked Submit and
    the form has been posted, before it prints the error message to the
    screen. Or is there a better way to do this? (I have done the separate
    form for verification method before. I am trying to see if there is a
    way to verify the form from within itself.)

  • Tim Van Wassenhove

    #2
    Re: Form Validation

    Jerim79 schreef:
    Here is my form:
    >
    <?php
    $Number;
    $Email;
    That's obosolete.. These days you get your input from the $_* arrays.
    (And later on you'll probably get it from the input_* functions)
    if (empty($Number) ){
    if (isset($_POST['Number'])) {
    echo "<form action=\"{$_SER VER['PHP_SELF']}\" method=\"POST\" >
    Using $_SERVER['PHP_SELF'] leads to security problems.. You can achieve
    the same result using '#' as action.

    <form action='#' method='post'>
    This works fine in that it catches what it needs to catch. However,
    obviously, it prints out the error message upon first loading the
    page.
    if ($_SERVER['REQUEST_METHOD '] == 'POST') {
    // the user posted something, process the form...
    } else {
    // the user getted something, display the form...
    }



    --
    Tim Van Wassenhove <url:http://www.timvw.be/>

    Comment

    • Toby A Inkster

      #3
      Re: Form Validation

      Jerim79 wrote:
      This works fine in that it catches what it needs to catch. However,
      obviously, it prints out the error message upon first loading the
      page.
      Do you plan on asking this same question every day?

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact
      Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

      * = I'm getting there!

      Comment

      Working...