facarroll Posted July 15, 2010 Share Posted July 15, 2010 I have a data form which sends to a mysql database. In the database I have 6 fields which are informed by check boxes and that dataevaluates to true("1") or false("0"). I am trying to write a short script to check whether one or more boxes were checked. I am new at this, but what I've written works well. The problem I have is that I don't know how to insert the script so that it allows the original script to continue if one of or more of the fields evaluates as true. This is what I have. There is something wrong at the end, I think. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/207818-php-if-elseif-else/ Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 Try something like: // perform query // assuming $data contains the row $valid = false; foreach ($data as $chkbox) { if ($chkbox == true) { $valid = true; break; } } if ($valid) { // do something here } Quote Link to comment https://forums.phpfreaks.com/topic/207818-php-if-elseif-else/#findComment-1086374 Share on other sites More sharing options...
facarroll Posted July 15, 2010 Author Share Posted July 15, 2010 Sorry Mr Adam, don't understand. I'll post my script here and see if you can figure out what I'm doing wrong. I've posted the working script with the part I cannot get to work is commented out. (lines 54-68) If I uncomment the lines the script stops. Any help? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/207818-php-if-elseif-else/#findComment-1086380 Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 Sorry I think I misunderstood what you were asking before (thought you were asking to validate there was at least 1 check box ticked for an entry in the database). Start again... --- Right then rather than giving each check box a unique name, store them as an array: <input type="checkbox" name="egroup[]" value="My check box" /> On the PHP side you can then spit out the error if the array is empty: if (empty($_POST['egroup'])) { exit('You must select a group. No selection has been made'); } The value for the check box is only posted if it's been checked. Quote Link to comment https://forums.phpfreaks.com/topic/207818-php-if-elseif-else/#findComment-1086460 Share on other sites More sharing options...
facarroll Posted July 20, 2010 Author Share Posted July 20, 2010 Got it with this. $nogroup = ((!$egroup1) && (!$egroup2) && (!$egroup3) && (!$egroup4) && (!$egroup5) && (!$egroup6)); $noequip = (!$equip); if ($nogroup){ $errorMsg .= "--- Student Group. (No data was forwarded to the database.)</font>"; } else if($noequip){ $errorMsg .= "--- Quiz Name. (No data was forwarded to the database.)</font>"; } else { Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/207818-php-if-elseif-else/#findComment-1088557 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.