Redirect URL keeps adding the same string

Here is the code:

    Dim MM_editRedirectUrl
    MM_editRedirectUrl =  Request.ServerVariables("URL") & "?"

    If (Request.QueryString <> "") Then
      If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
        MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
      Else
        MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString & "&index=" & (CStr(Int(Request.QueryString("index"))) + RecordExam_numRows)
      End If
    End If
    Response.Redirect(MM_editRedirectUrl)

The first time, it works, and I get this:

http://www.libertylabs.com/tana11/exama43w.asp?&EmpID=29&TestID=44&index=1

But the second time, I get this:

http://www.libertylabs.com/tana11/exama43w.asp?&&EmpID=29&TestID=44&index=1&index=2

When I want to get this:

http://www.libertylabs.com/tana11/exama43w.asp?&EmpID=29&TestID=44&index=2
MzLibertyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nap0leonConnect With a Mentor Commented:
Found a function on stack overflow that works nicely.
In this example, if index is not in the QueryString, it adds it as "index=3".
If index is in the URL, e.g., index=5, it adds 3 so it results in index=8

<%
Dim RecordExam_numRows
RecordExam_numRows = 3

Dim MM_editRedirectUrl
MM_editRedirectUrl =  Request.ServerVariables("URL") & "?"
%>

Path w/ ?: <%=MM_editRedirectUrl%><br/>

<%
Function updateQueryStringParam(key, value)
	Dim qs, x
	For Each x In Request.QueryString
		qs = qs & x & "="

		If x = key Then
			qs = qs & value & "&"
		Else
			qs = qs & Request.QueryString(x) & "&"
		End If
	Next

	If Len(qs) > 0 Then
		If Right(qs, 1) = "&" Then
			qs = Left(qs, Len(qs)-1)
		End If
	End If

	updateQueryStringParam = qs
End Function

If (Request.QueryString <> "") Then
	If (InStr(1, Request.QueryString, "index=", vbTextCompare) = 0) Then
		MM_editRedirectUrl = MM_editRedirectUrl & Request.QueryString & "&index=" & (CStr(Int(Request.QueryString("index"))) + RecordExam_numRows)
		'Response.Redirect(MM_editRedirectUrl)
	Else
		'increment index
		MM_editRedirectUrl = MM_editRedirectUrl & updateQueryStringParam("index", Int(Request.QueryString("index")) + RecordExam_numRows)
	End If
End If
%>

Finish: <%=MM_editRedirectUrl%><br/>

Open in new window

0
 
nap0leonCommented:
Firstly, this should be tagged with and placed in a topic for "Classic ASP" instead of "JavaScript".

Secondly, in what situation would you be running the code a second time?  The code does a redirect, it ought to run once and redirect to the new page.  Perhaps you are meaning that the code is running again after the page has redirected and it should not?

As for the issues in the code provided...
Your first "If" is checking to see if "Request.QieryString" is not empty.
Your second "If" is checking to see if the path you constructed does not contain a "?" in it... but you added a "?" to it when you created the variable, so it will always have one.

On the off-chance that you were intending to check to see if "index=" is in the QueryString (with the intention of adding it if it is not), then something like this would work:
<%
Dim RecordExam_numRows
RecordExam_numRows = 3

Dim MM_editRedirectUrl
MM_editRedirectUrl =  Request.ServerVariables("URL") & "?"
%>

Path w/ ?: <%=MM_editRedirectUrl%><br/>

<%
If (Request.QueryString <> "") Then
	If (InStr(1, Request.QueryString, "index=", vbTextCompare) = 0) Then
		MM_editRedirectUrl = MM_editRedirectUrl & Request.QueryString & "&index=" & (CStr(Int(Request.QueryString("index"))) + RecordExam_numRows)
		'Response.Redirect(MM_editRedirectUrl)
	Else
		MM_editRedirectUrl = MM_editRedirectUrl & Request.QueryString
	End If
End If
%>

Finish: <%=MM_editRedirectUrl%><br/>

Open in new window

0
 
MzLibertyAuthor Commented:
That works the first time, but only the first time...it gives me index #1 but I need it to go to index #2 the second time. It just keeps giving me index #1. The code I had gave me the second index, but kept the first one in there. I don't know which part of that code is causing it to keep the first index and then add the second...I'm just trying to piece this together as I obviously don't know what I'm doing :) Thanks!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nap0leonCommented:
If you are stuck at "1" then it is because you do not have a value for "RecordExam_numRows"

In your original code, you are adding "index=something" to the QueryString which already contains "index=something", so now it is in there twice.
0
 
MzLibertyAuthor Commented:
Yes, it is in there twice, that's the problem...but the second time, it does give me the correct number (2, then 3 and so on) but how do I make it only give me the 2 and not the 1 again?
0
 
MzLibertyAuthor Commented:
Thanks for your help. At least I'm getting closer to making it work.
0
All Courses

From novice to tech pro — start learning today.