13

私は次のフォームを持っています:

<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>

編集

送信前に 2 つの値を 1 つの連想配列に渡すことは可能ですか? 次の形式で渡したいと思います。

array('deptid'=>'deptname')

宛先のphpファイル(options.php)のスクリプトを変更することを避けるため、これが必要です

ありがとう。

4

7 に答える 7

26

これは純粋な HTML を使用したメソッドで、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>

これはPHPであなたを与えるでしょう:

$post_options = array(
    'options' => array(
        'deptid '=> '[that input element value]',
        'deptname' => '[that input element value]'
    )
);

次に、(サニタイズを含む)次のようにアクセスできます。

$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 
}

編集

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">

http://codepad.org/DtgoZGe7

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.

于 2012-12-27T07:12:40.000 に答える
6

$_POSTはすでに連想配列であり、フォームから取得したデータを既に保持しているため、それ以上複雑にしないことをお勧めします。$_POST

$myassoc = $_POST;
print_r($myassoc);

受け取る連想配列は、フォームの入力要素 (テキストエリアを含む) の name 属性と同じように編成され、名前が付けられます。

その他の洞察

あなたのコードを見ると、データが PHP サーバー側コードに到達したときにdeptnameデータを配置したいと考えています。deptidあなたができることは、それをキーに割り当てることですdeptid

$_POST['deptid'] = $_POST['deptname'];
$myassoc = $_POST;
print_r($myassoc);
于 2012-12-27T07:10:21.550 に答える
3
<form method="post">
    <input type="text" name="formdata['deptid']" />
    <input type="text" name="formdata['deptname']" />
    <input type="submit" />
</form>

<?php
    if(isset($_POST['formdata']))
    {
       $deptid = $_POST['formdata']['deptid'];
       $deptname = $_POST['formdata']['deptname'];
    }
?>
于 2012-12-27T07:14:11.973 に答える
1

適切な構造の JS オブジェクトを構築し、それを で JSON に変換しJSON.stringify()、POST してから を実行しますjson_decode($_POST['data'],true)

JS オブジェクトから PHP 関連配列への正確なコピーが作成されます。の 2 番目のパラメーターをドロップしてtrue、PHP オブジェクトを取得します。

于 2013-06-27T15:16:34.760 に答える
0

$_POSTはすでに連想配列です。

$_POST変数に割り当てるだけで、これから必要な形式の配列を再構築できます

$myarray = $_POST;

今 $myarray が必要です。してくださいvar_dump($myvar);

于 2012-12-27T07:23:48.220 に答える
-1

なぜそれをしたいのですか?ただし、次のようなフォームを介して「配列」を送信できます。

<form method="post">
    <input type="text" name="textboxes[]" />
    <input type="text" name="textboxes[]" />
    <input type="submit" />
</form>

<?php
    if(isset($_POST['textboxes']))
        var_dump($_POST['textboxes']);
?>
于 2012-12-27T07:10:55.243 に答える
-1
$deptid = $_POST['deptid'];
$array = array($$deptid => $_POST['deptname']);
print_r($array);
于 2012-12-27T07:13:15.613 に答える