...

View Full Version : Need to reset a variable if user hits 'Refresh'



BlasTech
11-09-2004, 09:21 AM
I've created a page that asks my user a bunch of random questions which are generated by some javascript. The questions are created in a form which is passed via post to a php page which handles all the information. I need to pass a variable that holds the numbers associated with the random questions so that I can access it later. I've got everything working, except for one problem. If the user hits the 'Refresh' or 'Reload' button on their browser the form is reloaded, which changes the random questions that are generated, but the variable that held the originally generated numbers stays the same. I need to know how to make the variable reset and get the new numbers if the user hits the refresh button. The variable and associated form field is located in this snippet of my code:




//function to put the random numbers into the form field seperated by a '-'
function store_questions()
{


var w = 0;
for(w=0; w<15;w++)
{

document.random_question.storage.value += used[w] + '-';

}

}



I need the document.random_question.storage.value to reset itself with the new numbers from used[w] that will come with the refresh. Currently with any refresh it keeps the original numbers that came with the form the first time it was created. Any ideas?

If the user just fills out the form without any refresh it works fine. As soon as they refresh it screws everything up.

Here is the part of the code that creates the form if that is of any help:




<form method="POST" action="random_questions.php" name="random_question">
<textarea name="description" cols="60" rows="2"></textarea><br><br>
<script type="text/javascript">
for (i=0;i<15;i++)
{
//write out the correct random question
document.write(random_nonrepeating_text[used[i]]);

//call to the function that will write out the form field
write_form();

}
</script>
<!-- this hidden field will hold the numbers of the questions the user has answered and will be uploaded to the script with the form-->
<input type ="hidden" name = "storage">
<script type="text/javascript">store_questions();</script><br>
<input type="submit" value="Press to Answer">



Thanks,

BlasTech

Mr J
11-09-2004, 02:03 PM
Make it a global variable set to zero outside the function

document.random_question.storage.value = 0

BlasTech
11-09-2004, 09:35 PM
I tried putting it in as a global in a couple different places and then it would fail to print out my form. Maybe someone could have a quick gander at the page and see if there are any ideas? I've commented out the variables as global in this one so it works, but maybe I have them in the wrong places? I'm pretty amateur in this coding game so there could easily be some stupid mistake I've made. And my code is probably ugly. But I'm trying. Anyway if someone could check it out and see if they can make anything out of the problem.

Script it Here (http://www.jrtfarm.com/random_questions.htm)

The easiest thing to do is to copy the Question number and put it into the form, then when you submit it the question number will pass, and you'll see that it is the same as the numbers in the variable passed. Then go back and try again, hit 'Refresh' once the form loads, copy and paste again, submit, and you'll see that the numbers are different.

I'm guessing I'm doing something really stupid, and I'm sure someone will figure it out quickly, so please, educate me!

Thanks again.

ps. that global variable suggested by Mr. J probably works, but maybe I just don't know where to place it? Any suggestions?

Mr J
11-09-2004, 11:07 PM
I forgot there was a form involved so you would get an error if that line was made global outside the functions.

Try running the following function onload

function reset_me(){

document.random_question.storage.value=""

}


add reset_me to the BODY onload event

If you want to see another way to generate random numbers see


www.huntingground.freeserve.co.uk/scripts/rdmgen.htm

BlasTech
11-10-2004, 01:18 AM
Tried your function, the function fires, but it doesn't change the values. But it does get reset if I post the form and go back to it like before. Just not on refresh. Any other ideas? Maybe I just put it in wrong? Here it is changed, I added an 'alert' so I would know if it fires the reset_me function or not, and it does.

Changed Script (http://www.jrtfarm.com/random_questions.htm)

Again, thanks for your efforts. If you don't have any other suggestions I'll just make and alert box telling my users not to reload the page or something.

-BlasTech

canadianjameson
11-10-2004, 02:18 AM
here's what i use



<script type="text/javascript">
function clearOnly()
{
document.productchoice.Quantity.value = "";
document.productchoice.Application_Information.value = "";
document.productchoice.Producer.selectedIndex = false;
document.productchoice.Product.selectedIndex = false;
// document.productchoice.Producer.selectedIndex = 2; <-- to have it reload with the 2nd option in Producer showing
// document.productchoice.FieldName.checked = false; <-- to unselect radio buttons or check boxes
}
</script>

...
<body onload="clearOnly()">


Cheers :)

BlasTech
11-10-2004, 05:26 AM
Excellent, thanks guys, got it working!

-BlasTech

canadianjameson
11-10-2004, 07:56 PM
awesome :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum