SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2005
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    changing a value of a variable?

    hi all,
    i have a login form on my site. if the user enters incorrect detaisl then the script will kick them back to the page they were viewing before submitting the login form (login box is on all pages) using:
    Response.Redirect(Request.ServerVariables("HTTP_REFERER") + "error=1")
    and then there is some client side js detecting the value of error and display a "login error" message. the thing is, if the user enters the wrong details again, then the HTTP_REFERER will add another "?error=1" to the string, so if the user enters wrong detaisl 4 times, the url is: http://www.mysite.com?error=3?error=3?error=3
    because there are more than 1 variable names the same, the clientside js doesnt execute. ive tried writing a bit of script to change the ? into & like this:
    if(header.indexOf("error") == -1){
    var prefix = "?"
    }else{
    var prefix = "&"
    }
    but this still doesnt work.
    can anyone suggest anything? many thanks
    - G

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Location
    New York
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do one of the following:

    1. Change your page to post back to the first page instead of building a querystring.

    2. Explicitly specify the page you are redirecting to (mypage.asp?error=1). Not as nice as simply using referer but I don't think you have a choice if you are going to use a querystring.

    HTTP_REFERRER is going to include the querystring each time you reference thereby resulting in appending error=1 with each redirect.

    Hope this helps

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Location
    New York
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do one of the following:

    1. Change your page to post back using a form to the first page instead of building a querystring.

    2. Explicitly specify the page you are redirecting to (mypage.asp?error=1). Not as nice as simply using referer but I don't think you have a choice if you are going to use a querystring.

    HTTP_REFERRER is going to include the querystring each time you reference thereby resulting in appending error=1 with each redirect.

    Hope this helps

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like you're using jscript.

    Give this a go:
    Code:
    function insertQSValue(url, qsName, qsValue) {
    	var urlParts = url.split("?");
    	
    	if (urlParts.length == 1) {
    		// there was no query string, simply add one
    		return url + "?" + qsName + "=" + qsValue;
    	} else {
    		// create an array of querystring name/value pairs
    		var pairs = urlParts[1].split("&");
    		
    		var foundVar = false; // whether the querystring already contained the value or not
    		
    		// go through the pairs array looking for a match to qsName
    		for (var i=0; i < pairs.length; i++) {
    			var pairParts = pairs[i].split("=");
    			if (pairParts[0].toLowerCase() == qsName.toLowerCase()) {
    				pairs[i] = qsName + "=" + qsValue;
    				foundVar = true;
    				break;
    			}
    		}
    
    		if (foundVar) {
    			return urlParts[0] + "?" + pairs.join("&");
    		} else {
    			return url + "&" + qsName + "=" + qsValue;
    		}
    	}
    }
    
    
    var newURL = insertQSValue(Request.ServerVariables("HTTP_REFERER"), "error", "1");
    Response.Redirect(newURL);

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2005
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well done jim, works great. thanks!
    - G


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •