Solved

Redirect URL keeps adding the same string

Posted on 2015-01-28
6
81 Views
Last Modified: 2015-01-28
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
0
Comment
Question by:MzLiberty
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:nap0leon
ID: 40575859
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
 

Author Comment

by:MzLiberty
ID: 40575904
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
 
LVL 18

Expert Comment

by:nap0leon
ID: 40575912
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MzLiberty
ID: 40575942
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
 
LVL 18

Accepted Solution

by:
nap0leon earned 500 total points
ID: 40576229
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
 

Author Comment

by:MzLiberty
ID: 40576437
Thanks for your help. At least I'm getting closer to making it work.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to refresh a page from a sub domain in ajax ? 34 43
JavaScript Loan Calculator Error 3 34
Send form to asp server side 6 26
Html using "Or" in condition 3 24
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question