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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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!! Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.