0

I have a script that calls another php page and passes values using PHP get.

The one variable, q is sent with the URL where str is a variable.

xmlhttp.open("GET","getdata.php?q="+str,true); 

I have a few multiple variables that I want to send in the URL string.

How can I send multiple variables.

Along the lines of:

xmlhttp.open("GET","getdata.php?q="+str+"y="+str2+"z="+str3,true); 

where the URL will then be somthing like

page.php?q=Peter&y=John&z=Smith
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Smudger
  • 10,451
  • 29
  • 104
  • 179

4 Answers4

9

You'll need to separate them with an ampersand, and probably URL-encode them too:

xmlhttp.open("GET","getdata.php?"
    + "q=" + encodeURIComponent(str)
   + "&y=" + encodeURIComponent(str2)
   + "&z=" + encodeURIComponent(str3), true);

Also, no problem ;)

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • +1 I just feel like downvoting all the other answers that don't mention encoding... – kapa Jul 05 '12 at 19:03
3

Seems you just forgot the ampersands:

xmlhttp.open("GET","getdata.php?q="+str+"&y="+str2+"&z="+str3,true);
                                         ^          ^

But, more important, you need to escape the strings:

str = encodeURIComponent(str);

before using them as url parameters. See also this article and that question on encode-functions.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

You need to add an ampersand between strings:

xmlhttp.open("GET","getdata.php?q="+str+"&y="+str2+"&z="+str3,true);
peacemaker
  • 2,541
  • 3
  • 27
  • 48
1

You're practically there, the only thing missing from your request/uri is the ampersand between the request parameters:

xmlhttp.open("GET","getdata.php?q="+str+"&y="+str2+"&z="+str3,true); 
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149