Checking input text?

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

    Checking input text?

    Hello

    I wondered if this script seemed correct? The user should enter a
    userid and password in two text fields, and then pass these values to
    the method login_user.

    Could I ask, is there any approach to, once the user clicks the submit
    button to test the userid and password has been entered, and the text
    fields are not empty.

    Thank you

    <?php

    function login_form(){
    ?>
    <html>
    <head>
    <title></title>
    </head>

    <body>

    <div align="center">
    <form method="post">
    <table border="1">
    <tr>
    <th align="right">E-mail</th>
    <td nowrap>
    <input name="userid" size="25">
    </td>
    </tr>
    <tr>
    <th align="right">P assword</th>
    <td nowrap>
    <input name="password" size="25">
    </td>
    </tr>
    <tr>
    <td align="center">
    <input type="submit" name="submit" value="Login">
    </td>
    </tr>
    </table>
    </form>
    <div>
    </body>
    </html>
    <?php
    }

    login_form();
    $user_info = login_user($_PO ST['userid'], $_POST['password'];
    ?>
  • C.J. Garibay

    #2
    Re: Checking input text?

    Antoni wrote:
    [color=blue]
    > Hello
    >
    > I wondered if this script seemed correct? The user should enter a
    > userid and password in two text fields, and then pass these values to
    > the method login_user.
    >
    > Could I ask, is there any approach to, once the user clicks the submit
    > button to test the userid and password has been entered, and the text
    > fields are not empty.
    >
    > Thank you
    >
    > <?php
    >
    > function login_form(){
    > ?>
    > <html>
    > <head>
    > <title></title>
    > </head>
    >
    > <body>
    >
    > <div align="center">
    > <form method="post">
    > <table border="1">
    > <tr>
    > <th align="right">E-mail</th>
    > <td nowrap>
    > <input name="userid" size="25">
    > </td>
    > </tr>
    > <tr>
    > <th align="right">P assword</th>
    > <td nowrap>
    > <input name="password" size="25">
    > </td>
    > </tr>
    > <tr>
    > <td align="center">
    > <input type="submit" name="submit" value="Login">
    > </td>
    > </tr>
    > </table>
    > </form>
    > <div>
    > </body>
    > </html>
    > <?php
    > }
    >
    > login_form();
    > $user_info = login_user($_PO ST['userid'], $_POST['password'];
    > ?>[/color]

    Antoni,

    The page that handles the userid/password POST data should look
    something like this:

    <?php
    $userid = $_POST["userid"];
    $password = $_POST["password"];

    if ($userid == "" || $password == "") {
    header("Locatio n: /path/to/login/page.php?error= 1");
    exit;
    }
    ?>

    Of course, you could do something more complicated than just checking
    for an empty string by using standard PHP functions (strlen(), etc...)
    or you could use regex.

    Another thing you might want to do is also validate the input on the
    client side using JavaScript. This lessens the load on your server if
    the visitor has JavaScipt enabled; otherwise, it'll get caught by PHP.

    C.J.

    Comment

    • Michael Fesser

      #3
      Re: Checking input text?

      .oO(C.J. Garibay)
      [color=blue]
      > $userid = $_POST["userid"];[/color]

      This will cause a notice (unknown index) if nothing was submitted.

      $userid = isset($_POST['userid']) ? $_POST['userid'] : '';

      Micha

      Comment

      • Matthias Esken

        #4
        Re: Checking input text?

        C.J. Garibay wrote:
        [color=blue]
        > header("Locatio n: /path/to/login/page.php?error= 1");[/color]

        You need a full URI here.

        | header("Locatio n: http://www.example.com/path/to/login/page.php?error= 1");

        Matthias

        Comment

        Working...