This is the basics:
PHP Code:
$chkAr = array("Employed", "Self", "Business", "Professional", "House", "Others");
//spoofing your POST vars
$_POST['cust_occup']["Employed"] = 'on' ;
$_POST['cust_occup']["Self"] = 'on' ;
foreach( $chkAr as $item){
if( array_key_exists($item, $_POST['cust_occup']))
echo 'selected = selected';
}
You can also make your chkAr work as a security white list too:
PHP Code:
$chkAr = array("Employed", "Self", "Business", "Professional", "House", "Others");
$_POST['cust_occup']["Employed"] = 'on' ;
$_POST['cust_occup']["Self"] = 'on' ;
// uncomment this row to see the script die
//$_POST['cust_occup']["Nasty_HACK"] = 'on' ;
$posted_keys = array_keys($_POST['cust_occup']);
if( array_diff($posted_keys, $chkAr)){
exit('Someone has been tampering with your values');
}
foreach( $chkAr as $item){
if( in_array($item, $posted_keys)){
echo $item. ' selected = selected' . PHP_EOL;
}else{
echo $item . PHP_EOL;
}
}
ps UNTESTED with real POST values btw ...
Bookmarks