下一篇 » « 上一篇

使用表单传递关联数组的例子

作者:爱好者    时间:2008-01-22    来源:php之家    点击:1764    本文共1篇文章 字体:[ ]

使用表单传递关联数组的例子

今天从 http://www.php.net的用户反馈处看到如下警告,讲到通过表单传递的关联数组不能被each()函数读取,我尝试了如下的操作,没有发现问题,结果完全正确。程序如下 www.444p.com php学习之家

//test1.php
<form action=test2.php method=post>
<input type=hidden name=var[8eccbad8e776e] value="php2000">
<input type=submit value=submit>
</form> php学习之家

//test2.php
<?
$msg = each($var);
echo $msg[0],$msg[1],$msg['key'],$msg['value'];
?>

www.444p.com版权所有

//output is
8eccbad8e776ephp20008eccbad8e776ephp2000

www.444p.com

原文 - http://www.php.net/manual/ref.array.php

本文来自 www.444p.com

mailto:garbepat@ifrance.com
08-Jan-2001 09:52

www.444p.com

Be careful if you want to submit a variable as an associative array.

If for some reason you use indexes that begin with a number - for instance some dynamically
display ID number :
<input type="textfield" name="vars[8eccbad8e776e]">
It seems to be a rare case but I needed to code that stuff.
The bug here comes from the fact the browser "urlencodes" the name of the field. www.444p.com php学习之家

The array is actually transmitted but you can't use the "each()" function on It. However I can
swear you the array is not empty and - that's worse - the correct data is stored at the correct
index (is knew It making 'echo $vars["8ecc..."]'). php学习之家http://www.444p.com

The less boring solution I found is to put an "_" before any index.
Then you will surely find a way to remove it - using "preg_replace" for instance. 本文来自 www.444p.com

责任编辑:semirock
发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


《PHP教程》点击排行