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.