douwaif22244 2013-09-28 20:04
浏览 28
已采纳

如何使用MYSQL信息在PHP中正确显示我的引号并将它们放在onclick事件中?

Paraphrasing...

OK i have information in a database it reads: This is random text with a "random quote"

$var = 'This is random text with a "random quote"'

onClick="show(\''.$var.'\')

but when displayed (viewing source) it shows:

onclick="show('This is random text with a  " random="" quote"')

I have tried mysqli_real_escape_string and str_replace, str_replace works if i am removing the "

Any help, alterations or reworks I would be grateful its driving me nuts right now.

  • 写回答

3条回答 默认 最新

  • duandu2980 2013-09-28 20:09
    关注

    If this for an HTML context, you should be using " to escape those. This is done with the htmlspecialchars function.

    If you want it as a JavaScript string, use json_encode on the string.

    mysqli_real_escape_string should be used only for database calls, and only as a last resort. Remember, parameterized queries are the best way to compose SQL statements.

    You really shouldn't need to ever set an onclick event directly. A library like jQuery has much better ways of doing this:

    $('#my_element').click(function() { show('...'); });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?