Hi.I want to do something like following code, but the check boxes appearing from given code are not checked after form submission.Here is my code:
$arr = $_POST['cust_occup'];
$cust_occup_array = explode(",",$arr);
$chkAr = array("Employed", "Self", "Business", "Professional", "House", "Others");
for($i=0; $i < count($chkAr); $i++){
echo '<input type="checkbox" name="cust_occup[]" id="cust_occup" value="'.$chkAr[$i].'"';
foreach($cust_occup_array as $arry){
if($chkAr[$i]==$arry){ echo 'checked="checked"'; }
else{ echo ''; }
}
echo ' > '.$chkAr[$i].' ';
}
Can anyone tell me how to explode $_POST string values to compare with an array?Any guidance will be very helpful for me.
Cups
2
This is the basics:
$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:
$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 …
Thanks for your reply Cups. I tried to implement your code in mine. But things dont seem work.May be I am going wrong somewhere.
Here is my new code:
<?php
$chkAr = array("Employed", "Self", "Business", "Professional", "House", "Others");
$_POST['cust_occup']["Employed"] = 'on' ;
$_POST['cust_occup']["Self"] = 'on' ;
$_POST['cust_occup']["Business"] = 'on' ;
$_POST['cust_occup']["Professional"] = 'on' ;
$_POST['cust_occup']["House"] = 'on' ;
$_POST['cust_occup']["Others"] = 'on' ;
for($i=0; $i < count($chkAr); $i++){
echo '<input type="checkbox" name="cust_occup[]" id="cust_occup" value="'.$chkAr[$i].'"';
if( array_key_exists($chkAr[$i], $_POST['cust_occup']))
echo 'selected = selected';
echo '> '.$chkAr[$i].' ';
}
?>
anita_86, checkboxes need checked=“checked”
Cups
5
foreach( $chkAr as $item){
echo '<input type=checkbox name=cust_occup['.$item.'] id=cust_occup value="' .$item .'" ' ;
if( in_array($item, $posted_keys) ) echo " checked = 'checked'";
echo ' />' . $item . PHP_EOL ;
}
// gives me
<input type=checkbox name=cust_occup[Employed] id=cust_occup value="Employed" checked = 'checked' />Employed
<input type=checkbox name=cust_occup[Self] id=cust_occup value="Self" checked = 'checked' />Self
<input type=checkbox name=cust_occup[Business] id=cust_occup value="Business" />Business
<input type=checkbox name=cust_occup[Professional] id=cust_occup value="Professional" />Professional
<input type=checkbox name=cust_occup[House] id=cust_occup value="House" />House
<input type=checkbox name=cust_occup[Others] id=cust_occup value="Others" />Others
Correction: as pointed out by @centered_effect; SB “checked”
Thanks for your valuable reply centered effect and Cups.I will do these changes and see if it works 