0

I have a php page with following components. Onclick the GET value should display in Textbox

JavaScript

category= Biscuits & Creams
document.location.href= "page.php?category="+category;

On Load GET parameter is Coming as follows,

page.php?category=Biscuits%20&%20Creams

Display the value in php page:

<input type="text" Value="<?php echo urlencode($_GET['category']);?>"/>

Output in Text Box is coming as

Biscuits instead of Biscuits & Creams

logan
  • 7,413
  • 36
  • 107
  • 178

2 Answers2

3

The PHP engine is using the & to separate the parameters in the query string. Encode it before using it in a URL.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
1

Add encode method in javascript, and in php use urldecode instead of urlencode

Javascript:

 category= 'Biscuits & Creams';
 document.location.href= "page.php?category="+encodeURIComponent(category);
Krish R
  • 22,188
  • 7
  • 49
  • 57