I'm trying to get data from php into my javascript. I'm using raw XMLHttpRequest.
It works fine BUT I'm having a parser error when trying to return <a>
links containing onclick
event with argument. For instance :
In PHP I have :
echo "<a href='#' onclick=myfunction('$data')>$data</a><br>";
In Javascript I have
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
document.getElementById("some_id").innerHTML=xhr.responseText;
}
}
BUT the google chrome console shows this in the HTML of the element "some_id
<a href="#" onclick="myfunction('John" Doe')>John Doe</a>
Instead of having 'John Doe'
we have 'John" Doe'
. I have figured that the white space between the names is responsible for this (i.e. with no blanks, the result would be 'John Doe'
How can I correct that ? (as the double quote in the javascript close the click ie clicking is triggers an error)
"`. Various escape and concat techniques work here as well, but I'll leave it to you to find out which technique works best for your style of coding. If using $data elsewhere still produces the double quote inside the name, have a look at what $data contains in the php script. – Shilly Jul 13 '16 at 13:48
";` – Shilly Jul 13 '16 at 14:16