Solved

Redirect URL keeps adding the same string

Posted on 2015-01-28
6
80 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 print dynamic Google map and directions? 1 23
Jquery 2 23
xjs: how to get sum of value in for loop 2 18
Button function on table is in trouble 3 20
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

856 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