Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
AND(&) Symbol Messing Up Params
Old 06-20-2007, 03:43 AM AND(&) Symbol Messing Up Params
Extreme Talker

Posts: 196
Trades: 0
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:
Code:
<input type="text" class="inputbox" value="pb&j" id="input_name" name="input_name" onblur="javascript:makeRequest('my_file.php?new=',this.value);"/>
Javascript:
Code:
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.');
 }
}
}
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-20-2007, 04:57 AM Re: AND(&) Symbol Messing Up Params
Extreme Talker

Posts: 196
Trades: 0
My solutions was to add the replace() function at line 2 of the following code.

Code:
function makeRequest(url, parameters) {
        parameters = parameters.replace(/&/g,"%26");
    http_request = false;

        ... rest of function
}
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 06-20-2007, 03:48 PM Re: AND(&) Symbol Messing Up Params
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
another way is with the 'escape' function. It will take care of other characters as well.
Code:
param = escape("hello&goodbye");
will set param to: "hello%26goodbye"

There is also an 'unescape' function that will do the reverse.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to AND(&) Symbol Messing Up Params
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.70313 seconds with 12 queries