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条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧