How to POST an associative array in PHP

2019-01-19 13:47发布

问题:

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.

回答1:

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

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.



回答2:

$_POST is already an associative array and I recommend you not to complicate things beyond that because $_POST already holds the data came from your form.

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

and the associative array that you will receive is organized and named same in the name attribute of the input elements in your form (including textarea)

Other Insights

As I see your code you want to put the deptname data to deptid as it reaches the PHP server-side code. well the thing you can do with is is just assign it to the key deptid

$_POST['deptid'] = $_POST['deptname'];
$myassoc = $_POST;
print_r($myassoc);


回答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'];
    }
?>


回答4:

Build a JS object with the appropriate structure, convert it to JSON with JSON.stringify(), POST it, and then do json_decode($_POST['data'],true).

You'll have an exact copy from JS object, to PHP associate array. Drop the second parameter of true to get a PHP object.



回答5:

$_POST is already an associative array.

You can rebuild an array of the form you need from this by just assigning $_POST to a variable

$myarray = $_POST;

Now $myarray is what you require. Do var_dump($myvar);.



回答6:

Why would you want to do that? But, you CAN send "arrays" through forms like this:

<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']);
?>


回答7:

$deptid = $_POST['deptid'];
$array = array($$deptid => $_POST['deptname']);
print_r($array);