PHP string / quotes problem driving me insane

amateursurgeon

Hot Metal and Methedrine
Apr 2, 2007
3,855
124
0
The Uncanny Valley
Please can someone point out what I'm missing here - I'm sure it's something obvious....

I'm trying to get my page to set a javascript variable based on form input from previous page (so that I can do user tracking in Clicky)

My code reads:

Code:
$ClickyUserName=str_replace(' ', '', $_REQUEST['CustomerName']); 

$ClickyCustom =  '
<script type="text/javascript">
  var clicky_custom = {};
  clicky_custom.session = {
    username: "'.$ClickyUserName.'"
  };
</script>';

echo $ClickyCustom;

But this always gives me an output of :

HTML:
<script type="text/javascript"> 
  var clicky_custom = {};
  clicky_custom.session = {
    username: ""
  };
</script>

I can't seem to get it to insert $ClickyUserName

When I echo $ClickyUserName by itself, I get the contents of the Request string back, so that's not the problem.

What am I missing?
 


try this..

PHP:
$ClickyCustom =  "
<script type=\"text/javascript\">
  var clicky_custom = {};
  clicky_custom.session = {
    username: \"".$ClickyUserName."\"
  };
</script>";

echo $ClickyCustom;
 
I tried the OP's code on PHP 5.2.10 and saw that the username was being filled in, but that the output would be partially duplicated:

HTML:
 <script type="text/javascript"> 
  var clicky_custom = {};
  clicky_custom.session = {
    username: "Hello"
  };
</script> 
  var clicky_custom = {};
  clicky_custom.session = {
    username: "Hello"
  };
</script>

I was able to make it stop that by adding a line break between the closing script tag and the single-quote.

PHP:
<?
$ClickyUserName=str_replace(' ', '', $_REQUEST['CustomerName']); 

$ClickyCustom =  '
<script type="text/javascript">
  var clicky_custom = {};
  clicky_custom.session = {
    username: "'.$ClickyUserName.'"
  };
</script>
';

echo $ClickyCustom;
?>

I have no idea why that stopped it... go figure.

Vad.
 
Thanks for the responses - it was a bug in Chrome... the code was working, but for some weird reason the username wasn't showing up under view source in Chrome.

Works elsewhere though. No idea why.