How to post associative array in PHP

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 two values ​​to one associative array BEFORE ? I would like to pass it in the following form:

 array('deptid'=>'deptname') 

I need this because I cannot change the script of the target php file (options.php)

Thanks.

+7
source share
7 answers

Here is a method using pure HTML that allows you to almost exactly where you want to be, and uses only 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> 

What PHP will give you:

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

What can you then (including disinfect), for example:

 $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 ... Do you want to reference the deptid in the input name attribute and use it to change the string for the 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?>"> 

What outputs:

 <input type="hidden" name="options[1]" value="Department of Silly Walks"> 

http://codepad.org/DtgoZGe7

The problem is that the value of $deptid becomes a value that does not actually have a direct name or reference. I think this is potentially problematic due to this abstraction of value from server to client and vice versa, so I would recommend what I have above. This is not a big difference in practice, but it is more or less self-documented.

Note that if you want to serialize the list of departments, it is a little more complicated. You can, for example, try the following:

 <input type="text" name="options[][deptid]" id="deptid" /> <input type="text" name="options[][deptname]" id="deptname" /> 

To add an indexed value for each input . However ... They would not be directly related. Thus, instead of two zeros, you get arrays for each key.

In this case, I would suggest using Javascript to add each new element of the input department so that you can specify each number, for example:

 <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 use the old-school POSTBACK method and use PHP to count $POST['options'] and manually add a new β€œline” of inputs with the same index. This is a common trap, so you just need to think about it if that is what you need at some point.

+20
source

$_POST already an associative array , and I recommend that you not complicate the situation, because $_POST already contains data received from your form .. p>

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

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

Other ideas

As I see your code, you want to put the deptname data in the deptid as it reaches the code on the server side of PHP. ok what you can do is just assign it to the deptid key

 $_POST['deptid'] = $_POST['deptname']; $myassoc = $_POST; print_r($myassoc); 
+5
source
 <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']; } ?> 
+3
source

$_POST already an associative array.

You can rebuild the array of the form you need by simply assigning $_POST variable

 $myarray = $_POST; 

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

0
source

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

You will have an exact copy from the JS object associated with the PHP array. Drop the second true parameter to get the PHP object.

0
source

Why do you need this? But you can send "arrays" through such forms:

 <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']); ?> 
-one
source
 $deptid = $_POST['deptid']; $array = array($$deptid => $_POST['deptname']); print_r($array); 
-one
source

All Articles