jquery - simple quote escape issue with pdo-php-javascript (ajax) -


Here's my problem:

I have a PHP script (one AJAX query from my database with PDO ):

  ... some code $ myString = $ pdoObject ['field']; ... some code   

is a single quote in the string: '

  Example: this is strange   

Later in this php script, I have put the string in a long string variable that I send back to my AJAX query:

  $ wholeString = " ; Td & gt; & lt; span title = '$ MyString' & gt; Some text & lt; / span & gt; & lt; / td & gt; & lt; / tr & gt; ";   

Then I send it back: json_encode ($ wholeString);

In my AJAX query, I just put the result in a jquery field:

  ... some code success: function (response) {$ ("# myField" ). Html (feedback); } ... Some code   

always talks tight on the quote:

  & lt; Tr & gt; & Lt; Td> & Lt; Span title = 'this' & gt; Some text & lt; / Span & gt; & Lt; / Td> & Lt; / Tr & gt;   

If I try to use htmlentities or htmlspecialchars before putting $ myString in the full string, it does not change anything ... I'm missing out on something. ..

Thank you

You basically have an html injection problem. You must use htmlspecialchars () to avoid all html metacars in your text, in which '

  $ wholeString = "& lt; tr & Gt; & lt; td & gt; & lt; span title = '' Htmlspecialchars ($ myString, ENT_QUOTES) "'& gt; Some text & lt; / Span & gt; & Lt; / Td> & Lt; / Tr & gt; ";   

As written in your code, you will be born:

  & lt; tr & gt; ; Td & gt; & lt; span title = 'this is weird' & gt; some text etc ...   

which will cause the span tag to parsor the browser

  & lt; span title = 'it' // "single" unknown random characteristic "weird" illegal single quote    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -