0

When we use GET method we use ambersand(&) sign to send the data to a particular script. I'm sending data to a php script called myscript.php by the GET method from a javascript like the following :

http.open('GET', 'Myscript.php'+ '?d=' + value1 + '&c=' + value2 + '&f=' + value3);

But if the value1 or value2 or value3 contains an & sign in their actual value (for example if somebody enter for the value1 garth&ggg&kkk then the Myscript.php can't handle the value1 properly because it contains an & sign in the actual data. It takes the value garth for value1.

My php code is as follows:

if (isset($_GET['d'])) {  $a =  $_GET['d'];}
Albzi
  • 15,431
  • 6
  • 46
  • 63
Explorer
  • 295
  • 1
  • 12

2 Answers2

1

You need to encode it properly before putting it into url to turn ampersand to %26:

  • in PHP: urlencode()
  • in javascript: encodeURIComponent()

You don't need to decode it in PHP, it will decode it for you to & character.

n-dru
  • 9,285
  • 2
  • 29
  • 42
0

JS you can use

encodeURIComponent(str);

Example

str="garth&ggg&kkk";
str=encodeURIComponent(str);

It will replace & with %26

user1844933
  • 3,296
  • 2
  • 25
  • 42