freelance84 Posted July 5, 2011 Share Posted July 5, 2011 Trying to grasp some more of the basics here... I have a simple script that will pass the value of a text box back to the server via AJAX. Upon testing the simple script i found that if i sent a long URL (like from google) with lots of different things in its GET... the php receives the said URL split into lots of different params: Going in: http://www.google.co.uk/#sclient=psy&hl=en&site=&source=hp&q=a+site&aq=f&aqi=&aql=&oq=&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=ab90aaaebdbbd579&biw=1680&bih=909 The JS: /*saving ther new URL*/ function saveURL(){ var newURL = document.getElementById('newURL').value; alert(newURL);//This alerts the full URL /*setting the variables*/ var url = "http://www.asite.net/mymarksProcessor.php"; var params = "newURL="+newURL; alert(params);//This alerts the full URL /*xml connection*/ xmlsetup(); xmlhttp.open("POST", url, true); /*Send the proper header information along with the request*/ xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", params.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { var response = xmlhttp.responseText; alert(response); } } xmlhttp.send(params); } The php: elseif(!empty($_POST)) { if(isset($_POST['newURL'])) { $newURL = mysql_real_escape_string(htmlentities($_POST['newURL'])); print_r($_POST); exit(); The Result from the the print_r($_POST) = Array ( [newURL] => http://www.google.co.uk/#sclient=psy [hl] => en [site] => [source] => hp [q] => a site [aq] => f [aqi] => [aql] => [oq] => [pbx] => 1 [bav] => on.2,or.r_gc.r_pw. [fp] => ab90aaaebdbbd579 [biw] => 1680 [bih] => 909 ) Really not sure what's going on here. Does anyone know why the var params = "newURL="+newURL; is being split at every & sign?? Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted July 5, 2011 Share Posted July 5, 2011 because each one of the URL parameters is accessible via the $_POST method, so naturally running a print_r will return these values in array format Quote Link to comment Share on other sites More sharing options...
freelance84 Posted July 5, 2011 Author Share Posted July 5, 2011 Do you know how I would pass the URL to the .php file without this happening? Should i be encoding the URL before sending it, and then unravelling at the server before processing? If so, how? Quote Link to comment Share on other sites More sharing options...
freelance84 Posted July 5, 2011 Author Share Posted July 5, 2011 ooo, think i just answered my own question there ha! http://www.javascripter.net/faq/escape.htm Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.