www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Form values lost when back (or javascript history -1) is used.

  1. #1
    Join Date
    Oct 2007
    Posts
    370

    resolved [RESOLVED] Form values lost when back (or javascript history -1) is used.

    My normal method of form handling is a file like contact.php with a handler like contacthandler.php. In contacthandler.php there would be a validation routine. If the form does not validate, I offer a javascript go-1 and the user can make corrections.

    My problem is with a script written by someone else. (I think it's related to the fact that it uses sessions.) I have added my form and everything works except that when you return to the previous page the form is blank. Quite frustrating for a long form. I know I can re-call the page with a submit button and hidden fields then fill in all the fields with
    Code:
    value="<?php echo($whatever);?>"
    But I'd rather find a way to keep those values in history.

    Can anyone give me a clue, or if it can't be done explain why?

    Tec.

  2. #2
    Join Date
    Apr 2008
    Posts
    182
    why not validate it first, so they dont have to go back. Its just a thought, I was messing around the other day and made a create an account/login script.
    I display a random 3 digit number for validation purposes. heres what I did

    JavaScript Validate
    <script>
    <!--
    function checker(){
    var pass = document.getElementById('userP').value;
    var passb = document.getElementById('userP_Conf').value;
    var email = document.getElementById('email').value;
    var emailb = document.getElementById('email_Conf').value;
    var theChecker = document.getElementById('val_fact').value;
    var theFact = document.getElementById('val_me').value;
    if(theChecker == theFact){
    if(pass == passb){
    if(pass.length >= 5){
    if(email == emailb){
    return true;
    }else{
    alert('Emails Dont Match');
    return false;
    }
    }else{
    alert('Password has to be atleast 6 characters');
    return false;
    }
    }
    else{
    alert('Pw Dont Match');
    return false;
    }
    }else{
    alert('Validation Number Incorrect, Try Again');
    document.getElementById('val_me').value = '';
    return false;
    }

    }
    -->
    </script>
    pass is the password, passb is a 'cofirm' password
    if they dont match pop up message with error

    email and emailb are the same way

    theFact is the value of the random number, its getting it from a hidden input,
    theChecker is what the user puts in if not the same error

    HTML
    <form method="post" action="addUser.php" onsubmit="return checker()">
    <table bgcolor="gray" style='border-left: 1px solid black;border-top:1px solid black; border-right:3px solid black; border-bottom:3px solid black;'>
    <tr>
    <td>
    UserName:
    </td>
    <td>
    <input type="text" name="userN" id="userN" />
    </td>
    </tr>
    <tr>
    <td>
    Password:
    </td>
    <td>
    <input type="password" name="userP" id="userP" /><br>
    </td>
    </tr>
    <tr>
    <td>
    Confirm Password:
    </td>
    <td>
    <input type="password" name="userP_Conf" id="userP_Conf" /><br>
    </td>
    </tr>
    <tr>
    <td>
    Email Address:
    </td>
    <td>
    <input type="text" name="email" id="email" /><br>
    </td>
    </tr>
    <tr>
    <td>
    Confirm Email:
    </td>
    <td>
    <input type="text" name="email_Conf" id="email_Conf" /><br>
    </td>
    </tr>
    <tr>
    <td>
    Validate:
    </td>
    <td>
    <input type="text" name="val_me" id="val_me" />
    </td>
    </tr>
    <tr>
    <td colspan='2' align="right">
    <b>Validation #: <?php $aVar=rand(100, 999); echo $aVar; ?></b>
    <input type="hidden" name="val_fact" id="val_fact" value='<?php echo $aVar ?>' />
    </td>
    </tr>
    <tr>
    <td colspan='2' align="right">
    <input type="submit" value="submit" />
    </td>
    </tr>
    </table>
    </form>
    Sorry about all the table b.s.
    Okay so when you submit it
    it validates first and if any of the info is not correct
    it returns false and does not go on to submit
    this is done by the onsubmit="return checker()"
    <form method="post" action="addUser.php" onsubmit="return checker()">
    I hope this helps some

    regards
    Marcus

  3. #3
    Join Date
    Oct 2007
    Posts
    370
    Thanks Marcus.

    I have seen similar scripts, but I prefer server side validation.
    Besides that, My normal method is a lot less scripting and the values are usually there when the back button is clicked, so I don't have to fill them back in. (and I can re-use my existing code)

  4. #4
    Join Date
    Oct 2007
    Posts
    370
    I while back, I found the solution to this and just now got around to posting it here.
    Put this code
    PHP Code:
    <?php session_cache_limiter('private-no-expire'); ?>
    at the top of your form page script. Then when your form page is hit with the back button or a javascript go -1 the previous values will be there.

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