/    Sign up×
Community /Pin to ProfileBookmark

[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);?>”[/CODE]

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@marcusamiApr 22.2008 — 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>
[/QUOTE]

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>
[/QUOTE]


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="[B]return[/B] checker()"

<form method="post" action="addUser.php" onsubmit="return checker()">
[/QUOTE]


I hope this helps some

regards

Marcus
Copy linkTweet thisAlerts:
@TecBratauthorApr 22.2008 — 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)
Copy linkTweet thisAlerts:
@TecBratauthorJun 22.2011 — I while back, I found the solution to this and just now got around to posting it here.

Put this code [code=php]<?php session_cache_limiter('private-no-expire'); ?>[/code]
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.
×

Success!

Help @TecBrat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...