如何在PHP中发布关联数组 [英] How to POST an associative array in PHP
问题描述
我有以下形式:
< form action =options.phpmethod =post>
< input type =textname =deptidid =deptid/>
< input type =textname =deptnameid =deptname/>
< input type =submitname =submitid =submitvalue =save/>
< / form>
编辑
是否可以将这两个值传递给一个关联数组 BEFORE 提交?
我想以这种形式传递它:
array('deptid'=>'deptname')
我需要这个,因为我避免修改目标php文件的脚本(options.php)
谢谢。
options.phpmethod =post> 哪一个可以在PHP中给你: 然后您可以(包括清理)访问权限,例如: 编辑 或...您想引用 / p> 问题在于
< input type =textname =options [deptid]id =deptid/>
< input type =textname =options [deptname]id =deptname/>
< input type =submitname =submitid =submitvalue =save/>
< / form>
$ post_options = array(
'options'=> array(
'deptid'=>'[input element value]',
'deptname'=>'[input element value]'
)
);
$ post_options = array('options');
if(is_numeric($ post_options ['deptid']&& $ amp; $ post_options ['deptid']> 0){
//做任何
}
if(is_string($ post_options ['deptname']&& strlen($ post_options ['deptname']> 2)){
//做任何
}
输入名称
属性中的 deptid
,并使用它修改部门名称?这似乎表明了这样的情况:
<?php
$ deptid = 1;
$ deptname ='Silly Walks部门';
?>< input type =hiddenname =options [<?= $ deptid?> ]value =<?= $ deptname?>>
< input type =hiddenname =options [1]value =愚蠢行走部门>
$ deptid
值变成了一个实际上没有直接命名或引用的值。我认为这是由于从服务器到客户端的价值抽象而实现的可能存在的问题,所以我会推荐我在顶端有什么。这在实践中并没有太大的区别,但它或多或少是自我记录的。
< input type =textname =options [] [deptid] id =deptid/>
< input type =textname =options [] [deptname]id =deptname/>
为每个输入
。但是......他们不会直接关联。所以,你会得到两个零索引的数组。每个键。
在这种情况下,我建议使用Javascript来添加每个新部门的 input
元素,因此您可以给每个元素设置一个数字:
<输入类型=textname =options [0] [deptid]id =deptid/>
< input type =textname =options [0] [deptname]id =deptname/>
< br />
< input type =textname =options [1] [deptid]id =deptid/>
< input type =textname =options [1] [deptname]id =deptname/>
< br />
< input type =textname =options [2] [deptid]id =deptid/>
< input type =textname =options [2] [deptname]id =deptname/>
< br />
< input type =textname =options [3] [deptid]id =deptid/>
< input type =textname =options [3] [deptname]id =deptname/>
或者使用旧式的POSTBACK方法并使用PHP来计数 $ POST ['options']
和手动添加具有相同索引的新输入行。这是一个常见的陷阱,所以你只需要考虑一下,如果这是你在某个时间点之后。
I have the following form:
<form action="options.php" method="post">
<input type="text" name="deptid" id="deptid" />
<input type="text" name="deptname" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
EDIT
Is it possible to pass the two values into one associative array BEFORE submission ? I would like to pass it in this form:
array('deptid'=>'deptname')
I need this because I avoid to modify the script of the destination php file(options.php)
Thanks.
Here is a method using pure HTML that get's you nearly exactly where you want to be, and only uses HTML:
<form action="options.php" method="post">
<input type="text" name="options[deptid]" id="deptid" />
<input type="text" name="options[deptname]" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
Which would give you in PHP:
$post_options = array(
'options' => array(
'deptid '=> '[that input element value]',
'deptname' => '[that input element value]'
)
);
Which you can then (including sanitizing) access such as this:
$post_options = array('options');
if (is_numeric($post_options['deptid'] && $post_options['deptid'] > 0) {
// Do whatever
}
if (is_string($post_options['deptname'] && strlen($post_options['deptname'] > 2)) {
// Do whatever
}
EDIT
Or... You want to reference the deptid
in the input name
attribute and use it to modify the row for a department name? Which seems to indicate something like this:
<?php
$deptid = 1;
$deptname = 'Department of Silly Walks';
?><input type="hidden" name="options[<?=$deptid?>]" value="<?=$deptname?>">
Which outputs:
<input type="hidden" name="options[1]" value="Department of Silly Walks">
The problem with this is that the $deptid
value becomes a value that's not actually directly named or referenced. I think this is potentially problematic to implement due to this abstraction of the value from the server to the client and back, so I would recommend what I have at the top instead. It's not much of a difference in practice, but it's more or less self-documenting.
Note, if you wanted to serialize a list of departments, it's a little trickier. You might, for instance, try this:
<input type="text" name="options[][deptid]" id="deptid" />
<input type="text" name="options[][deptname]" id="deptname" />
Which would add an indexed value for every input
. However... They were would not be directly associated. So you would get, instead, two zero-indexed arrays for each key.
What I would suggest in this case is to use Javascript to add each new department's input
elements, so you can give each a number like:
<input type="text" name="options[0][deptid]" id="deptid" />
<input type="text" name="options[0][deptname]" id="deptname" />
<br/>
<input type="text" name="options[1][deptid]" id="deptid" />
<input type="text" name="options[1][deptname]" id="deptname" />
<br/>
<input type="text" name="options[2][deptid]" id="deptid" />
<input type="text" name="options[2][deptname]" id="deptname" />
<br/>
<input type="text" name="options[3][deptid]" id="deptid" />
<input type="text" name="options[3][deptname]" id="deptname" />
Or do the old-school POSTBACK method and use PHP to count $POST['options']
and "manually" add a new "row" of inputs with the same index. It's a common trap, so you just have to think about it if this is what you're after at some point.
这篇关于如何在PHP中发布关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!