AND(&) Symbol Messing Up Params

Discussion in 'JavaScript' started by cesarcesar, Jun 19, 2007.

  1. #1
    I'm using the following AJAX code to send GET vars to my processing PHP file. This works great as is except that it will not accept a *&* (AND symbol) in the text input. It trys to pass what is after the *&* as a new GET var. I totaly get what is happening, just don't get how to fix it. I hope my example code is sufficient.

    HTML:
    
    <input type="text" class="inputbox" value="pb&j" id="input_name" name="input_name" onblur="javascript:makeRequest('my_file.php?new=',this.value);"/>
    
    Code (markup):
    Javascript:
    
    var http_request = false;
    function makeRequest(url, parameters) { //alert(parameters)
    	http_request = false;
    	if (window.XMLHttpRequest) { // Mozilla, Safari,...
    		http_request = new XMLHttpRequest();
    		if (http_request.overrideMimeType) {
    			// set type accordingly to anticipated content type
    			//http_request.overrideMimeType('text/xml');
    			http_request.overrideMimeType('text/html');
    		}
    	} else if (window.ActiveXObject) { // IE
    		try {
    			http_request = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try {
    				http_request = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e) {}
    		}
    	}
    	if (!http_request) {
    		alert('Cannot create XMLHTTP instance');
    		return false;
    	}
    	http_request.onreadystatechange = alertContents;
    
    	// FOR POST VARS
    	//http_request.open('POST', url, true);
    	//http_request.send(parameters);
    
    	// FOR GET VARS
    	http_request.open('GET', url + parameters + "&time=" + get_date(), true);
    	http_request.send(null);
    }
    
    function alertContents() {
    if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        //alert(http_request.responseText);
        result = http_request.responseText;
        document.getElementById('myspan').innerHTML = result;
     } else {
        alert('There was a problem with the request.');
     }
    }
    }
    
    Code (markup):
     
    cesarcesar, Jun 19, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    krt, Jun 19, 2007 IP
  3. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I see the code you have suggested, i just am a little unfamiliar with JS and not sure how to implement it.
     
    cesarcesar, Jun 20, 2007 IP
  4. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    My solutions was to add the replace() function at line 2 of the following code.

    
    function makeRequest(url, parameters) {
            parameters = parameters.replace(/&/g,"%26");
    	http_request = false;
    
            ... rest of function
    }
    
    Code (markup):
     
    cesarcesar, Jun 20, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    All is fine now?

    Also, I recommend:
    parameters = parameters.replace("&","%26");
    Code (markup):
    Do not use regex unless it is needed as it complicates things and takes longer to execute (not that the difference is noticeable here :p).
     
    krt, Jun 20, 2007 IP
  6. DavidMerrilees

    DavidMerrilees Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You should use the escape(string) and unescape(string) methods.
     
    DavidMerrilees, Jun 20, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    There they are, I knew JS had this function somewhere. It also has encodeURI()
     
    krt, Jun 20, 2007 IP