douyue2313 2010-08-26 12:40
浏览 64
已采纳

PHP表单变量和POST - HTML编码字符串

I am not strong on my PHP knowledge, but I have never seen this before. In a config file, there are a list of options defined in an array like so:

$testarray[] = "None";
$testarray[] = "Item 1 with normal text";
$testarray[] = "Item 2® with html encoded string";
$testarray[] = "Item3® with another html encoded string";

So now, when the form is generated, it does a simple for each loop to create a list of radio buttons:

foreach ( $testarray as $key=>$item){
echo '<div id="padBottom"><input type="radio" name="formItem"';
    if ( $item == $_SESSION['ss']['selection']) echo ' checked="checked"';
    echo ' value="' . $item. '" />' . $item. '</div>';
}

So far so good, the form generates like it should. The part that is not working is the If statement portion. On the page that this form posts to, it does a simple call to set $_SESSION['ss']['selection'] = $_POST['formItem']; When this happens, the value that goes into session is the actual registered trademark symbol and not '&reg;' as I would expect. As a result, if you select an item with the HTML encoded entity in it, we are not getting a match and your selection appears to be lost. In this example, choosing the first or second option results in the proper radio button being selected - if you choose option 3 or 4, then no selection appears to have been made when you return to this screen.

Addtional info - the charset for this page is UTF-8 if that makes any difference here.

Things I have tried

  1. htmlspecialchars on the POST variable in the next page: result, nothing, still showing actual symbol.
  2. htmlspecialchars on the form value for each radio option: result, I get &amp;reg; as part of my value which doesn't display well.
  • 写回答

2条回答 默认 最新

  • doushi1996 2010-08-26 12:43
    关注

    Some characters (including ampersands) have special meaning in HTML, you have to represent them as entities if you don't want that special meaning to take effect.

    In this case, it is the variable you are inserting into the value attribute that you need to encode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器