views:

22

answers:

2

I've an input text field.

On press enter, i'm performing the following action

function doWork(){    
         httpObject = getHTTPObject();
         if (httpObject != null) {
            link = "message.php?nick="+nickName+"&msg="+document.getElementById('msg').value;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setOutput;
            httpObject.send(null);
         }
      }

What I want is to "urlencode" the value.

How should I do that??

+1  A: 

Just wrap the value with "escape" as in:

link = "message.php?nick="+nickName+"&msg="+escape(document.getElementById('msg').value);
orvado
+1  A: 

Use the JavaScript function encodeURIComponent

link = "message.php?nick="+nickName+"&msg="+encodeURIComponent(document.getElementById('msg').value);

Dan Grossman
at the receiving file (ie message.php here), i use $_GET['msg']. Now how do I store the decoded value into a php variable? Something like, $message = decodeURIComponent($_GET['msg'])
ptamzz
You shouldn't need to decode it -- PHP will decode it for you when parsing the URL (or the request body, in the event of a POST) and populating the request variables.
cHao
im not sure of that. If I'm inserting the variable string $_GET['msg'] to the database.. so won't it insert the special chars in the encoded format?? thanks
ptamzz