I am trying to pass a string into a javascript from PHP but am failing miserably. From testing I can see its the whitespace that is making my test fail. How do I encode to pass to javascript properly I tried %20 and a few more nothing seems to work.
Full Source
<script async src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function DemoOne(text) {
$('#PageView').load('test.php?text=' + text);
}
</script>
<?php
$message="hello";
echo " <a href=\"javascript:DemoOne('$message');\" ><input class='btn' type='button' value='Test'></a>
<div id='PageView'></div>";
?>
Test Output test.php
<?php
echo $_GET['text'];
?>
FOLLOW UP
Adding the code below still fails it seems only removing spaces will allow this above example to work.
a href=\"javascript:DemoOne(".htmlspecialchars(json_encode($message)).");\" ><input type='button' value='Submit'></a>
<div id='PageView'></div>