-2
"sample1.php?q="+str 

This code is for sending single parameter i need to send multiple variables

user1122910
  • 117
  • 3
  • 14

5 Answers5

0

You can also create a url object for this and use the class methods:

const myURL = new URL('https://www.example.com');

myURL.searchParams.set('fooName', 'fooValue');

console.log(myURL);

// Output = https://www.example.com/?fooName='fooValue'
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '21 at 15:05
0

Define "multiple values". If you mean more than one variable, you can do something like:

$url = "sample1.php?q=".$str."&second=".$str2;
enygma
  • 684
  • 4
  • 6
0

try this code:

<script>
function make_full_url(Page_URL,Params) {
  Page_URL += "?";
  for Key in Params 
    Page_URL += Key+"="+Params[Key]+"&";
  return Page_URL;
}

//test
Page_URL = "sample1.php";
Params = new Array();
Params["q"] = Str;
Params["something"] = "something";

Full_URL = make_full_url(Page_URL,Params);
</script>
jondinham
  • 8,271
  • 17
  • 80
  • 137
0

For HTML:

"sample1.php?q=" + str + "&otherParam=" + other;

For XHTML/XML:

"sample1.php?q=" + str + "%26otherParam=" + other;
austincheney
  • 1,189
  • 9
  • 11
  • "&" is for showing the ampersand in html text, not for JS – jondinham Mar 09 '12 at 13:03
  • @mplungjan No, that is incorrect. & is HTML encoded entity for "&". The correct URI encoding is %26. Passing "&" into a URI creates the parameter name "amp;otherParam" if not decoded by HTML first. – austincheney Mar 09 '12 at 17:39
  • Not convinced... http://stackoverflow.com/questions/275150/xhtml-and-ampersand-encoding – mplungjan Mar 10 '12 at 17:42
  • @mplungjan You missed a key statement in the winning answer of that stackoverflow question: _The encoding of & as & is required in HTML, not in the link._ This question here is not about HTML. This is a JavaScript question. – austincheney Mar 16 '12 at 19:57
0

i hope i understand you correct... make a array with your values and loop over the key values to concat your url string

$values = array('q' => $str, 'val2' => $val2);
$url = 'sample1.php';
$pos = 0;
foreach($values as $key => $value) {
    $url .= ($pos>0 ? '&' : '?').$key.'='.urlencode($value);
    $pos++;
}
silly
  • 7,789
  • 2
  • 24
  • 37