Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to reset a variable if user hits 'Refresh'

    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:

    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:

    Code:
    <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

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Make it a global variable set to zero outside the function

    document.random_question.storage.value = 0
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder
    Join Date
    Aug 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    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?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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


    http://www.huntingground.freeserve.c...pts/rdmgen.htm
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    New Coder
    Join Date
    Aug 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    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

  • #6
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    here's what i use

    Code:
    <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
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #7
    New Coder
    Join Date
    Aug 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent, thanks guys, got it working!

    -BlasTech

  • #8
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    awesome
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)


  •  

    Posting Permissions

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