View Full Version : using onChange page gets stuck in refresh loop

09-21-2005, 03:15 PM
I have a page where I have a few different select boxes with onChange. If you change the select once and give it a value, everything works fine. If you then want to change it to another answer, it gets stuck in a refreshing loop. It just keeps refreshing the page and all that I can do is click the refresh button at the top. If I click this button, the page refreshes back to the first option picked.

Does anyone know how I can set up my page so that I can change the select answer and the page will update properly. The code that I am using:
The java at the bttom of the page.
<script type="text/javascript">
function updateForms() {

The start of the select box:
Response.Write "<select onchange=""updateForms()"" name=""txtIBMName"" class=""medium"" style=""font-family:Verdana, Arial;font-size:10"">" & chr(13)

Any help is greatly appreciated.

09-21-2005, 04:38 PM
This code should not hurt anything.
What else do you have in there that you didn't post, such as how you select the option?

09-21-2005, 04:56 PM
At the top of the page, I set the different variables to session variables.
The body:
<body onLoad="setTimeout('restoreValues()',50);setTimeout('watchTimes()', 1200);">

Then I use the select statement like I posted earlier:
Response.Write "<select onchange=""updateForms()"" name=""txtIBMName"" class=""medium"" style=""font-family:Verdana, Arial;font-size:10"">" & chr(13)

Just a drop down box that when an option is select, the page is suppose to refresh with the new option selected in the box.
I do have about 4 different select statements on the page, all using onchange.

The end of the page:
<script type="text/javascript">

function updateForms() {
//window.location.reload( true );

function restoreValues() {
if ('<% response.write Request.Form("txtIBMName") %>' != ''){ document.forms['page1'].txtIBMName.value = '<% response.write session("IBMName") %>';}
if ('<% response.write Request.Form("txtIBMQuantity") %>' != ''){ document.forms['page1'].txtIBMQuantity.value = '<% response.write Request.Form("txtIBMQuantity") %>';}

var lastTime = '';

function watchTimes(){

if (lastTime != ''){
if (lastTime != document.forms['page1'].txtIBMName.value){ document.forms['page1'].submit(); }
lastTime = document.forms['page1'].txtIBMName.value;

setTimeout('watchTimes()', 1000);


This is all I have in my sight. It works correctly the first time it is run. After that first time, if you want to change the select, it just gets stuck. I have no idea. Please help.

09-21-2005, 05:14 PM
Okay, I figured as much.
You're using client-side code to change the values. So, onchange fires which submits the form. And it happens all over again.

Why are you using client-side script for setting up the selects when you have a server-side language available?
This is not the normal way such things are done. The selects would be set up with ASP.

That said, you need to put code in your function that does the selection to set a global variable to some value that means it changed that select field (not the user), then have your onchange function check for that before it submits. Or stop event propagation. Or something. Didn't think it through yet.
Whatever way you do it, you need to distinguish your code changing the selects from the user changing them.

09-21-2005, 05:27 PM
Um. I coded it that way because that is all I know how to do. Not really sure what you are talking about. I have the session variables that are being set with the values. I am kind of new to this stuff. If you wouldnt mind explaining this a little more, I would greatly appreciate it. Thank you.

09-21-2005, 10:36 PM
Hey, no problem.
Sometimes there's a reason people code something a certain way.

If I were trying to set select elements to the values in a session, and I was using ASP to generate the page to begin with, I would do something like

' get the session stuff and so on
myVar = Session("whatever")

<select name="mySelect">
<option value="one"<% if myVar="one" then response.write "selected" end if%>>one</option>
<option value="two"<% if myVar="two" then response.write "selected" end if%>>two</option>
<option value="three"<% if myVar="three" then response.write "selected" end if%>>three</option>

Oh, and why are you using session variables?
If it's to save a user's choices, but you only need them in the next/same page like this, then you're making extra work for yourself.
You only need session variables to save things that a page several clicks away need, or if you want to imitate form persistence.