robert_gsfame Posted March 16, 2010 Share Posted March 16, 2010 I have this code <?php $country=$_POST['country']; if($country[0]=="brazil"){ $brazil="CHECKED";} if($country[1]=="paraguay"){ $paraguay="CHECKED";} if($country[2]=="germany"){ $germany="CHECKED";} if($country[3]=="argentina"){ $brazil="CHECKED";} if($country[4]=="japan"){ $brazil="CHECKED";} ?> <html> <head> </head> <body> <form name=form1 action=$_SERVER['PHP_SELF'] method=POST> <input type =checkbox name=country[] value=brazil <?php echo $brazil;?>>Brazil <input type =checkbox name=country[] value=paraguay <?php echo $paraguay;?>>Paraguay <input type =checkbox name=country[] value=germany <?php echo $germany;?>>Germany <input type =checkbox name=country[] value=argentina <?php echo $argentina;?>>Argentina <input type =checkbox name=country[] value=japan <?php echo $japan;?>>Japan <input type=submit value=okay> </form> </body> </html> The problem is when i tick on all checkboxes and click on the submit button, i only found that only 3 checkboxes being ticked although i have ticked 4 checkboxes...??Which code is wrong?? thx Link to comment https://forums.phpfreaks.com/topic/195448-checkbox-not-return-the-value/ Share on other sites More sharing options...
hcdarkmage Posted March 16, 2010 Share Posted March 16, 2010 I have this code <?php $country=$_POST['country']; if($country[0]=="brazil"){ $brazil="CHECKED";} if($country[1]=="paraguay"){ $paraguay="CHECKED";} if($country[2]=="germany"){ $germany="CHECKED";} if($country[3]=="argentina"){ $brazil="CHECKED";} if($country[4]=="japan"){ $brazil="CHECKED";} ?> <html> <head> </head> <body> <form name=form1 action=$_SERVER['PHP_SELF'] method=POST> <input type =checkbox name=country[] value=brazil <?php echo $brazil;?>>Brazil <input type =checkbox name=country[] value=paraguay <?php echo $paraguay;?>>Paraguay <input type =checkbox name=country[] value=germany <?php echo $germany;?>>Germany <input type =checkbox name=country[] value=argentina <?php echo $argentina;?>>Argentina <input type =checkbox name=country[] value=japan <?php echo $japan;?>>Japan <input type=submit value=okay> </form> </body> </html> The problem is when i tick on all checkboxes and click on the submit button, i only found that only 3 checkboxes being ticked although i have ticked 4 checkboxes...??Which code is wrong?? thanks my guess would be these lines here: if($country[0]=="brazil"){ $brazil="CHECKED";} if($country[1]=="paraguay"){ $paraguay="CHECKED";} if($country[2]=="germany"){ $germany="CHECKED";} if($country[3]=="argentina"){ $brazil="CHECKED";} // <--- Here if($country[4]=="japan"){ $brazil="CHECKED";} // <--- Here you have $brazil being checked 3 times, so it only reads 3 checks, not 4 or 5. Link to comment https://forums.phpfreaks.com/topic/195448-checkbox-not-return-the-value/#findComment-1027022 Share on other sites More sharing options...
aeroswat Posted March 16, 2010 Share Posted March 16, 2010 What are you trying to do? This is a bad way of writing your code. First you should use a function like in_array() instead of checking the specific array key for a value because the array will only hold whatever has been checked. That's what is going wrong here. So instead of if($country[0]=="brazil") you should have if(in_array("brazil",$country)) my guess would be these lines here: if($country[0]=="brazil"){ $brazil="CHECKED";} if($country[1]=="paraguay"){ $paraguay="CHECKED";} if($country[2]=="germany"){ $germany="CHECKED";} if($country[3]=="argentina"){ $brazil="CHECKED";} // <--- Here if($country[4]=="japan"){ $brazil="CHECKED";} // <--- Here you have $brazil being checked 3 times, so it only reads 3 checks, not 4 or 5. This too Link to comment https://forums.phpfreaks.com/topic/195448-checkbox-not-return-the-value/#findComment-1027023 Share on other sites More sharing options...
robert_gsfame Posted March 16, 2010 Author Share Posted March 16, 2010 Thx aeroswat, yeah i should use in_array..... Thank you for your help!! Link to comment https://forums.phpfreaks.com/topic/195448-checkbox-not-return-the-value/#findComment-1027026 Share on other sites More sharing options...
aeroswat Posted March 16, 2010 Share Posted March 16, 2010 thanks aeroswat, yeah i should use in_array..... Thank you for your help!! Not a problem man. Hope we helped. Please mark the thread as solved if you got it working Link to comment https://forums.phpfreaks.com/topic/195448-checkbox-not-return-the-value/#findComment-1027028 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.