SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form submission error trap methods

    I am working on some error trapping for several forms on my site. After visiting a bunch of websites, I've noticed 2 common methods of displaying error messages.

    1. display an error box on a new page and force the user to hit the <back> button

    2. display the form again with appropriate error text and pre-filled fields.

    I have part of the error on the new page working but I'm running into the infamous no contents in the form after going back. I saw a couple posts referencing changing the cache limiter in php.ini but I was wondering if there is a way of doing it without changing php.ini or making the form a GET intead of a POST?

    There are some useability issues with forcing the user to hit the back button -- some just don't want to bother.

    Is there a way to display the form w/original contents and error messages 'without' having to echo the entire form twice? My form has about 50 fields so echoing it twice seems kindof redundant.

    Sorry for the lengthy post...
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: form submission error trap methods

    Originally posted by jdulberg
    Is there a way to display the form w/original contents and error messages 'without' having to echo the entire form twice? My form has about 50 fields so echoing it twice seems kindof redundant.
    you don't actually have to echo the form twice... i'm assuming by echoing you mean coding the same form twice only the 2nd time you add the error messages...

    you can use the same form only have errorflags (use an array) which you can then tell if a particular form field has an error and then you display the error msg (which also can be stored in the array )

  3. #3
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm assuming by echoing you mean coding the same form twice only the 2nd time you add the error messages...
    Yes, your right. I should have been more clear.

    How would I go about calling up the form with the contents already included?

    I currently have the form setup so it only displays if the user hasn't submitted...

    PHP Code:
    if (!$submit) {
    echo 
    "<input type=text ....>";
    //etc.

    I'm assuming that I would need to change the if statement around somewhat to catch the error message?

    Thanks...
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what i do is have a function to display the form and pass an array containing errorneous fields to it...
    PHP Code:
    ///////////////////////////////////////////////////////////////////////////////
    // Function: void display_form(array $err)
    // Purpose: displays form for signing guestbook
    // Parameters:
    //    $err - array containing erroroneous fields and error messages
    // Returns: nothing
    // Global references:
    //        $PHP_SELF
    //        $HTTP_POST_VARS
    ///////////////////////////////////////////////////////////////////////////////
    function display_form($err)
    {
        global $PHP_SELF;
        global $HTTP_POST_VARS;
    ?>

    <div align="center">
    <h1>Leave a Message</h1>
    <?php
    if(count($err))
    // error in previous submission
    {
        echo 
    "<span class=\"error\">There was an error in your previous submission. Please re-enter the fields highlighted in red.</span>\n";
    }
    ?>  
    <form name="sign" method="post" action="<?php echo $PHP_SELF?>">
    <input name="action" type="hidden" value="process_form">

    <!-- form table -->
    <table width="<?=$GLOBALS["OB_table_width"]?>" height="50%" cellspacing="1" cellpadding="5">
        <tr>
            <th width="30%" align="right"><?php if(in_array("username",$err)) echo "<span class=\"error\">"?>Name:<?php if(in_array("username",$err)) echo "</span>"?></th>
            <td><input name="username" type="text" size="20" maxlength="30" value="<?=$HTTP_POST_VARS["username"]?>"><font color="red">*</font></td>
        </tr>
    // blah blah...
    of course you can do better than this if you think thru it carefully... you can use a form object too...

  5. #5
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code is getting me on the right track. One problem that I can forsee though is that I have about 15 different forms on my site. How could I reuse the error function in all of the forms?

    Also once I do the error checking, what is the best way to add it to the array? ie.

    if (!isset($variable)) {
    //errorstuff
    }

    Thanks again
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jdulberg
    One problem that I can forsee though is that I have about 15 different forms on my site. How could I reuse the error function in all of the forms?

    Also once I do the error checking, what is the best way to add it to the array? ie.

    if (!isset($variable)) {
    //errorstuff
    }
    since $err is an array, all you have to do is add the fieldname of the erroneous inputs to $err... then it can be reused no matter what form you have... of course it can be done much better but i've yet to think of that (lazy )

    but i don't quite understand what you're getting at when u say "what is the best way to add it to the array?"...

  7. #7
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh ok i misunderstood your question...

    well to reuse the function for different forms you... well, you can't i guess... for that you'd need a better function which can automatically create the form for you...

  8. #8
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay... I messed around with things a bit and moved the php stuff to the top and kept the html form at the bottom. I have part of the validation working however if more than 1 error exists, it still only prints the 1st one.

    Below are 2 example places where there would be an error... if I leave them both blank, they should both give an error message but I'm only gettig the 1st one.

    PHP Code:
    $error=array();
    if (strlen($username)< 3) {
        $error['username']="Username must be more than 3 characters";
    }
    elseif (strlen($password)< 3) {
        $error['password']="Password must be more than 3 characters";
    }


    <input type="text" name="username" value="<?=$username;?>">
    <? if ($error['username']) echo "<br>".$error['username'];?>

    <input type="text" name="password" value="<?=$password;?>">
    <? if ($error['password']) echo "<br>".$error['password'];?>
    Am I assigning errors to the array incorrectly?

    Thanks for your help
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the if-elseif control construct will only execute the statements in one of the if clauses if that particular condition is satisfied...

    ie. when you do this:
    PHP Code:
    $error=array();
    if (
    strlen($username)< 3) {
        
    $error['username']="Username must be more than 3 characters";
    }
    elseif (
    strlen($password)< 3) {
        
    $error['password']="Password must be more than 3 characters";

    only username gets assigned becos it's condition is satisfied... PHP then ignores the elseif part since it has satisfied the first condition... anyway it should make sense in plain english if you read it out loud enuff

    something like this would work
    PHP Code:
    $error=array();
    if (
    strlen($username)< 3) {
        
    $error['username']="Username must be more than 3 characters";
    }
    if (
    strlen($password)< 3) {
        
    $error['password']="Password must be more than 3 characters";


  10. #10
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i found a short tutorial on control structures at O'Reilly's PHP devCenter for you
    here

  11. #11
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I feel like a moron. I can't believe I didn't catch that error. Such a simple mistake -- guess those are always the one's that mess things up.

    Thanks again for your help. I have it all working now
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  12. #12
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't worry... everybody has a moronic stumble now and then... even the geeks do it (hehe just kidding)

  13. #13
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to bother you again... I just noticed a problem when I add the error trapping to a "modify" section.

    Since the edit page takes the data from the database, when the error trapping finds an error, the resulting edit page has the wrong form data.

    So on first entry to the edit page (from DB):
    <input type=text name=email value=\"$email\"> is test@test.com

    But when an error happens, it should be the data that the user just entered, not the database data.

    After error trapped:
    <input type=text name=email value=\"$email\"> is bademail.test.com

    Do I need to create an additional form or change the field names?

    Or should I just not bother to error trap modified data?

    Thanks again !!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net


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
  •