javascript - 处理 HTML 表单中的 JSON

标签 javascript php html json

我问这个问题是因为我正在使用一种有效的技巧,但我认为我将来会出现问题。

我使用 SimpleXML 和 DOM 在 php 中生成一个 html 表单来管理源(一个包含所需所有表单的 html 文件)。所以我拿到了表格并填写了一些属性。这是一个例子:

<input type="hidden" name="arquivo_tipos" value="{arquivo_tipos}" />

{arquivos_tipos} 是一个 JSON 字符串,在双引号内不起作用。但是,如果我创建这样的源代码:

<input type="hidden" name="arquivo_tipos" value='{arquivo_tipos}' />

经过php处理后,会返回双引号(我从很多中挑选表单作为xml节点)。所以我的解决方案是用这种脚本替换该属性:

html = html.replace('"{arquivo_tipos}"', '\'[{"ext":"jpg","nome":"Imagem JPEG"},{"ext":"jpeg","nome":"Imagem JPEG"},{"ext":"gif","nome":"Bitmap GIF"},{"ext":"png","nome":"Imagem PNG"}]\'');

这是 JavaScript,但在 php 中我对 str_replace 使用相同的技巧。

重点是,这是最终的代码,所以它可以工作,但闻起来如果另一个进程采用这个结果,它将来会失败。有更好的方法、正确的方法吗?

最佳答案

使用php htmlspecialchars函数,可以转义

double-quotes to &quot;
single-quotes to &#039;

http://tw1.php.net/htmlspecialchars

关于javascript - 处理 HTML 表单中的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295360/

相关文章:

php - Mysql存储过程where子句

javascript - 动态地,使用 for 循环检查单选按钮

html - Angular Js - 在 Angular TreeView 指令中呈现 html

javascript - 使用自动完成和相同的 ID 填充多个字段

javascript - 在 Facebook 选项卡内提供反向链接

javascript - Polymer 1.0 更改 <template> dom-repeat 中与 Javascript 的绑定(bind)

html - 为什么这个转换不利用 gpu(或者利用它)?

javascript - jQuery 自动完成 - 未捕获的类型错误 : undefined is not a function

php - 在 Laravel 5 中验证动态添加的输入字段

php - 使用数组搜索 MySQL 数据库