www.webdeveloper.com
Results 1 to 11 of 11

Thread: PHP and HTM Validation Problem

  1. #1
    Join Date
    May 2006
    Posts
    6

    PHP and HTM Validation Problem

    hi

    i have the following code which sets up text boxes for input

    <form method="post" action="ex20-03.php">
    <table border="0" width="90%" class="txtSt">
    <tr><td colspan="2" class="txtSt2">General Information</td>
    <td colspan="2" class="txtSt2">Login Information</td></tr>
    <tr><td >Name</td><td>
    <input type="text" name="name" class="butSt">&nbsp;&nbsp;&nbsp;</td>
    <td >User Id</td>
    <td ><input type="text" name="user_id" class="butSt"></td></tr>
    <tr><td >Gender</td>
    <td><input type="text" name="gender" class="butSt"></td>
    <td >Password</td>
    <td ><input type="password" name="pass_id" class="butSt"></td></tr>
    <tr><td >Age</td>
    <td><input type="text" name="age" class="butSt"></td>
    <td >Confirm Password</td>
    <td ><input type="password" name="con_pass_id" class="butSt"></td></tr>
    <tr><td >Address</td>
    <td><input type="text" name="add01" class="butSt"></td>
    <td colspan="2" class="txtSt2">Credit Card Information</td></tr>
    <tr><td >Address</td>
    <td><input type="text" name="add02" class="butSt"></td>
    <td >Credit Card Type</td>
    <td ><input type="text" name="card_type" class="butSt"></td></tr>
    <tr><td >City</td>
    <td><input type="text" name="city" class="butSt"></td>
    <td >Card Number</td>
    <td ><input type="text" name="card_no" class="butSt"></td></tr>
    <tr><td >Country</td>
    <td><input type="text" name="country" class="butSt"></td>
    <td >Expiry Date</td>
    <td ><input type="text" name="expiry_date" class="butSt"></td></tr>
    <tr><td >Postal Code</td>
    <td><input type="text" name="post_code" class="butSt"></td>
    <td colspan="2" class="txtSt2">Contact Information</td></tr>
    <tr><td >Email Address</td>
    <td><input type="text" name="email" class="butSt"></td>
    <td >Phone Number</td>
    <td ><input type="text" name="phone_no" class="butSt"></td></tr>
    </table><br /><br />
    <input type="submit" value="Register" class="butSt">
    <input type="reset" value="Clear" class="butSt">
    </form>


    now this seems to work no problem in setting up the web-page however when i come to validate the contents it does not work

    when the submit button is triggered it calls some php like bellow

    <?php
    if ( (trim($name)=="") || (trim($gender)=="") || (trim($email)=="") ||
    (trim($age)=="") || (trim($add01)=="") ||
    (trim($add02)=="") || (trim($city)=="") ||
    (trim($country)=="") || (trim($post_code)==""))
    {
    echo "Error.. General Information is not complete!";
    exit();
    }


    there is more than this but this is the first part and it never gets past this

    even though there is data in every text field its stops here and says that some fields ar empty



    can anyone please help

    thanks scott

    probaly something really simple for you guys

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Unless register_globals is on (which it really should not be), you'll need to reference the values from the $_POST array:
    PHP Code:
    if ( (trim($_POST['name'])=="") || (trim($_POST['gender'])=="") || 
    If that's not the case, or if you've copied the $_POST array vales to scalar variables, then the only other thing is to double-check that you don't have a spelling error somewhere, as comparing an uninitialized variable to "" will always return true.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    my guess is that is has something to do with register_globals and/or superglobals
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    PS: a cleaner way to check the required values in the $_POST array might be:
    PHP Code:
    $required = array('name''gender''email''age''add01''add02''city''country''post_code');
    foreach (
    $required as $ix)
    {
      if(empty(
    trim($_POST[$ix])))
      {
        echo 
    "Error.. General Information is not complete!";
        exit();
      }

    Also, should "add02" be required? (Usually this is an optional field for things like apartment numbers.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    Beware: empty() also returns True if the value is 0.

    PHP Code:
    if (trim($_POST['some_field']) == ""
    is therefor better (imho)
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  6. #6
    Join Date
    May 2006
    Posts
    6
    Quote Originally Posted by themarty
    Beware: empty() also returns True if the value is 0.

    PHP Code:
    if (trim($_POST['some_field']) == ""
    is therefor better (imho)

    i tried this but is now comes up with a parse error


    really dont know why my original code doesnt work

    thanks for the help so far guys

    im sure we'll crack this

  7. #7
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    i tried this but is now comes up with a parse error
    You should give a bit more code. The error is not always on the line php indicates it is.

    really dont know why my original code doesnt work
    Because you're using old-fashioned code. It only works if the register_globals setting is switched on, which is often not the case. (read the two links i posted earlier)
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    When you can't figure out why something like that is working, it's often useful to add some temporary debug code. For instance, you might want to echo out each of the variables be checked in your if statement to see what values they have. Then if only one of them is coming up empty, you've narrowed down the problem and know where to focus your attention.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    May 2006
    Posts
    6
    here is the new problem code
    PHP Code:
    <?php


    if ( (trim($_POST['name']) == "")  || (trim($_POST['gender']) == "")  || (trim($_POST['email']) == "")  || 
         (
    trim($_POST['age']) == "")    || (trim($_POST['add01']) == "")  ||
         (
    trim($_POST['add02']) == "")   || (trim($_POST['city']) == "")  ||
         (
    trim($_POST['country']) == "")  || (trim($_POST['post_code']) == "") )
    {
       echo 
    "Error.. General Information is not complete!"
       exit();
    }
    else if ( (
    trim($_POST['user_id']) == "") || (trim($_POST['pass_id']) == "") || 
              (
    trim($con_pass_id)=="") ) 
    {
       echo 
    "Error.. Login Information is not complete!"
       exit();
    }
    else if ( (
    trim($_POST['card_type']) == "") || (trim($_POST['card_no']) == "") || 
              (
    trim($_POST['expiry_date']) == "")) 
    {
       echo 
    "Error.. Credit Card Information is not complete!";
       exit();
    }
    else if (
    trim($_POST['phone_no'] == "")
    {
       echo 
    "Error.. Contact Information is not complete!"
       exit();
    }
    else if (
    trim($_POST['con_pass_id'] != (trim($_POST['pass_id']) ) { 
       echo 
    "Error.. Two Passwords do not matched!";
       exit();
    }
    else 
    {
      
       echo 
    "Registration Successful! <br /> Enjoy Your Shopping";
       exit();
    }
    ?>
    the error i get is

    Parse error: parse error, unexpected '{' in line 32


    thanks for the help so far guys
    Last edited by NogDog; 06-07-2006 at 01:59 AM. Reason: added php tags

  10. #10
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    Parse error: parse error, unexpected '{' in line 32
    So, what does this error tell you? That on line 32 PHP encounters a { where it didn't expect one. If it expected something else, you probably didn't close a certain delimiter. So, first step is to use color-coding:

    PHP Code:
    if (
        (
    trim($_POST['name']) == "") || (trim($_POST['gender']) == "") || (trim($_POST['email']) == "") ||
        (
    trim($_POST['age']) == "") || (trim($_POST['add01']) == "") || (trim($_POST['add02']) == "") || 
        (
    trim($_POST['city']) == "") || (trim($_POST['country']) == "") || (trim($_POST['post_code']) == "")
    )
    {
        echo 
    "Error.. General Information is not complete!";
        exit();
    }
    else if ( (
    trim($_POST['user_id']) == "") || (trim($_POST['pass_id']) == "") || (trim($con_pass_id)=="") )
    {
        echo 
    "Error.. Login Information is not complete!";
        exit();
    }
    else if ( (
    trim($_POST['card_type']) == "") || (trim($_POST['card_no']) == "") || (trim($_POST['expiry_date']) == ""))
    {
        echo 
    "Error.. Credit Card Information is not complete!";
        exit();
    }
    else if (
    trim($_POST['phone_no'] == "")
    {
        echo 
    "Error.. Contact Information is not complete!";
        exit();
    }
    else if (
    trim($_POST['con_pass_id'] != (trim($_POST['pass_id']) )
    {
        echo 
    "Error.. Two Passwords do not matched!";
        exit();
    }
    else
    {
        echo 
    "Registration Successful! <br /> Enjoy Your Shopping";
        exit();

    If you forgot a double quote, for example, with colorcoding you would see this immediately. At first glance this appears not to be the case though
    As you can see, i also indented your code. This is a very important practice that helps debugging a lot. Furthermore, consistancy in your code is important too. In your code for example, you sometimes put the { on the same line as the (else)if and sometimes on the line below it. That makes it very hard to read and track down errors. Better choose one or the other.
    A final point of critic: don't use so many exits. If you keep using it this way, you'll come to a point where you have a very big piece of code and a blank screen and no way of finding out what went wrong where. It's much better to force your code down the right path by using things like if/else structures.

    Now, how to find this bug. As i mentioned before: the most likely mistake is that you forgot to 'close ' something. So, start by the line directly above it. Nothing supsicious there. The line above that: there's double quotes ... but they seem to be ok too. On the line above that there's some single quotes and also quite a lot of (). The single quotes seem to be ok, so count the amount of ( and compare that with the amount of ).
    etc. etc.

    that's how you debug
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Parentheses problem: This line...
    PHP Code:
    else if (trim($_POST['con_pass_id'] != (trim($_POST['pass_id']) ) { 
    ...should be...
    PHP Code:
    else if (trim($_POST['con_pass_id']) != trim($_POST['pass_id']) ) { 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles