note 101825 deleted from reserved.variables.post by danbrown
| From: | danbrown@php.net | Date: | Thu, 13 Jan 2011 15:16:02 +0000 |
| Subject: | note 101825 deleted from reserved.variables.post by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175708@lists.php.net to get a copy of this message | ||
Note Submitter: me (at) dylanrosario (dot) com
----
It seems like no one here has posted a function to handle multi-select boxes or checkbox groups in
php $_POST
If you need a dynamic way to get the POST values of a form when :
1] you need to build a SQL statement from a POST
2] multiple select options stored in same column
3] the form input fields names can be arbitrary
I Use this nice little function to handle this requirement :
<?php
// save as myPOSTProcessor.php
function getPost($pst){
foreach ($pst as $key => $value) {
$v = $value;
$k = $key;
if(is_array($v)){
$z = implode(",",$v);
$kv[] = array($k,$z);
}else{
$kv[] = array($k,$v);
}
}
return $kv;
}
if($_POST){
$arr = getPost($_POST);
print_r($arr);
}
?>
+++++++++++
An example form that uses this :
<form method="POST" name="form1" id="form1"
action="myPOSTprocessor.php">
<input name="textfield" type="text" id="textfield"
value="text" />
<textarea name="textarea" id="textarea" cols="45"
rows="5">textarea</textarea>
<input name="checkbox" type="checkbox" id="checkbox"
checked="checked" />
<input name="checkboxu[]" type="checkbox" id="checkbox2"
value="1" checked="checked" />
<input name="checkboxu[]" type="checkbox" id="checkbox3"
value="2" checked="checked" />
<input name="checkboxu[]" type="checkbox" id="checkbox4"
value="3" checked="checked" />
<input type="radio" name="radio" id="radio1"
value="radio1" />
<input name="radio" type="radio" id="radio2"
value="radio2" checked="checked" />
<input type="radio" name="radio" id="radio3"
value="radio3" />
<select name="xselect[]" size="3" multiple="multiple"
id="select">
<option value='11111' selected="selected">option 1</option>
<option value='22222' >option 2</option>
<option value='33333' selected="selected">option 3</option>
</select>
<input type="image" name="imageField" id="imageField"
value="image" src="random.gif" />
<input type="submit" id="button" value="Submit" />
</form>