http://localhostr.com/files/V1ruKKj/capture.png
I want to rewrite all these ifs to something more manageable for lots of form fields.
I know it's not right but I want to rewrite it to something like this:
$fields = array();
function infcheck($var)
{
if ( !empty( $var ) )
{
$fields[$var] = $var;
}
}
infcheck( $_POST['streetname'] );
infcheck( $_POST['city'] );
infcheck( $_POST['state'] );
Basically when I run infcheck() I want the output to look like this:
Assuming $_POST['streetname'] is "Circle Street"
streetname => "circle street"
for a full example:
$fi开发者_运维技巧elds = array();
infcheck( $_POST['streetname'] );
infcheck( $_POST['city'] );
//would be:
$fields = array(streetname => 'Circle Street', city => 'New York City');
I guess what I'm having trouble with is keeping the name of the form when $_POST['formname']
is turned into a variable.
What you're basically doing is this:
$fields = array_filter($_POST);
If you want to limit the $fields
array to certain keys in a predetermined list and skip anything in the $_POST
array that's not on that list, you can do something like this:
$whitelist = array('streetname', 'city', ...);
$fields = array_filter(array_intersect_key($_POST, array_flip($whitelist)));
You could just put the array of values you want to pass through a foreach loop..
foreach($_POST as $key => $value){
$fields[$key] = $value;
}
精彩评论