6

Possible Duplicate:
How to encode a URL in JavaScript?

I am trying to send a url using the following code to a php code, but as the url include &a=12&b=4 once I get the value of the "a" variable in my php code the last part of address is removed.

url = http://www.example.com/help.jpg?x=10&a=12&b=4 but the url that I get in my php file is http://www.example.com/help.jpg?x=10 (&a=12&b=4 is removed, I know the reason is that javascript,ajax mix it up with the url address and do not know its just a value but do not know how to solve it)

         function upload(url){

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("output").innerHTML= xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","Photos.php?a="+url,true);
            xmlhttp.send();
     }        


   if(isset($_GET["a"]))
   {
       $Address = $_GET["a"];
       echo $Address;

   }

output is >>> " http://www.example.com/help.jpg?x=10" but it should be http://www.example.com/help.jpg?x=10&a=12&b=4

Community
  • 1
  • 1
Saeed Pirdost
  • 167
  • 3
  • 12
  • 1
    Check this out: http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – John V. Jan 07 '13 at 21:33
  • 1
    Closely related: http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – gd1 Jan 07 '13 at 21:33
  • 1
    @gd1 Wow, that's an interesting coincidence. – John V. Jan 07 '13 at 21:34
  • 2
    As a comment I would say that if you're posting something you should use the POST http method, not GET. Considering your url problem I guess the solution relies into using javascript `url_encode` methods combined with `$_REQUEST` array in php. – Sebas Jan 07 '13 at 21:35

2 Answers2

5

You need to encode the parameter

xmlhttp.open("GET","Photos.php?a="+encodeURIComponent(url),true);
Musa
  • 96,336
  • 17
  • 118
  • 137
0

You need to encode the URL. You can use encodeURIComponent(str) and encodeURI(str) like:

var eurl = encodeURIComponent("http://www.example.com/help.jpg?x=10&a=12&b=4");
xmlhttp.open("GET","Photos.php?a=" + eurl, true);
AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
bPratik
  • 6,894
  • 4
  • 36
  • 67