kirstenmarie Posted May 8, 2014 Share Posted May 8, 2014 I've never used is_array before or keys to check for the value. I have a multi check box form. Some or all of the boxes can be checked or none is ok. I am trying to create exception handling before the values hit the database to make sure that only the value of the check box is being passed. Would my code look like.. if is_array($_POST[interest] = true) value [1] == "sewing"->sewing if value [1] !=="sewing" thrown an exception value [2] == "hiking" ->hiking Am I on the right track? profile.php Quote Link to comment https://forums.phpfreaks.com/topic/288329-is_array/ Share on other sites More sharing options...
PravinS Posted May 8, 2014 Share Posted May 8, 2014 you can check it like this if (is_array($_POST['interest']) && count($_POST['interest']) > 0) { // code here } Quote Link to comment https://forums.phpfreaks.com/topic/288329-is_array/#findComment-1478689 Share on other sites More sharing options...
cyberRobot Posted May 8, 2014 Share Posted May 8, 2014 if (is_array($_POST['interest']) && count($_POST['interest']) > 0) { // code here } Note that the above code will give an undefined variable notice if you show all errors. Instead, you could use something like this: if(isset($_POST['interest'])) { //process the "interest" values here } When the POST variable is set, it should be an array as long as the corresponding checkboxes are named appropriately. Quote Link to comment https://forums.phpfreaks.com/topic/288329-is_array/#findComment-1478711 Share on other sites More sharing options...
cyberRobot Posted May 8, 2014 Share Posted May 8, 2014 Would my code look like.. if is_array($_POST[interest] = true) value [1] == "sewing"->sewing if value [1] !=="sewing" thrown an exception value [2] == "hiking" ->hiking Am I on the right track? Assuming that visitors can check more than one "interest" box, you could process the selected values like this: if(isset($_POST['interest']) && is_array($_POST['interest'])) { foreach($_POST['interest'] as $currInterest) { echo "<div>$currInterest</div>"; } } Of course, you'll need to adapt the code to fit your needs. Note that I added an is_array() test to make sure the "interest" variable is an array before using the foreach loop. That way you don't get errors if the user decides to tamper with the data submitted. Quote Link to comment https://forums.phpfreaks.com/topic/288329-is_array/#findComment-1478716 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.