Solved

Redirect URL keeps adding the same string

Posted on 2015-01-28
6
75 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:MzLiberty
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your help. At least I'm getting closer to making it work.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now