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,946
  • 36
  • 114
  • 185

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
  • 776,304
  • 153
  • 1,341
  • 1,358
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,583
  • 7
  • 50
  • 59