[英]How to store values with the special characters into HTML hidden fields using PHP
这可能是一个非常简单的问题,但我无法弄清楚。 我需要使用PHP将一些包含特殊字符的值存储到HTML隐藏字段中,例如5' 5''
指示人的身高。
该值存储在MySql数据库中。 我尝试过这样的事情
echo "<input type='hidden' id='ed_your_height' name='hd_your_height'
value=".html_entity_decode($your_heigth)."/>";
和
echo "<input type='hidden' id='ed_your_height' name='hd_your_height'
value=".htmlentities($your_heigth)."/>";
但这两种情况下的值都是5'
而不是5' 5''
。 我需要在下拉列表中显示这些值并进行一些比较。
然后,如何将带有特殊字符的值存储到HTML隐藏字段中(从数据库中检索)?
将htmlentities()
和html_entity_decode()
与正确的标志一起使用。
在客户端
<input type="text name="fieldname" value="<?php echo htmlentities($string, ENT_QUOTES, 'utf-8'); ?>">
在服务器端:
$string = html_entity_decode($_POST['fieldname'], ENT_QUOTES, 'UTF-8');
使用htmlspecialchars
。
value="<?=htmlspecialchars($string)?>"
假设short_open_tag处于打开状态(错误),或者您使用的是PHP 5.4(良好)。 否则,请使用完整的<?php echo
形式。
PS:首先遇到问题的原因是因为您没有引用属性-如果使用浏览器的“查看源代码”选项,则会看到输出为value=5' 5''
因此将其解释为值'5
和一个自定义布尔属性5''
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.