doforumda Posted February 16, 2010 Share Posted February 16, 2010 hi i am testing checkboxes with php. I have a form there are 6 checkboxes. what i want is when user does not check any checkbox then it should not return false. The form which i have has this problem when i dont select any box and press submit then it displays error. how can i fix it so it should not display error. html file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action="checkboxesprocess.php"> <p>Interested In: <label> <input type="checkbox" name="interest[]" value="m" id="checkbox" /> Men</label> <label> <input type="checkbox" name="interest[]" value="w" id="checkbox2" /> Women</label> </p> <p>Looking For: <label> <input type="checkbox" name="looking[]" value="f" id="checkbox3" /> Friendship</label> <label> <input type="checkbox" name="looking[]" value="d" id="checkbox4" /> Dating</label> <label> <input type="checkbox" name="looking[]" value="r" id="checkbox5" /> A Relationship</label> <label> <input type="checkbox" name="looking[]" value="n" id="checkbox6" /> Networking</label> </p> <p> <label> <input type="submit" name="button" id="button" value="Submit" /> </label> </p> </form> </body> </html> php file <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $interest = $_POST['interest']; $look = $_POST['looking']; $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); foreach($interest as $int) { $queryinterest = mysql_query("INSERT INTO interestin VALUES ('','$int')"); } foreach($look as $lo) { $queryinterest = mysql_query("INSERT INTO lookingfor VALUES ('','$lo')"); } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/ Share on other sites More sharing options...
gwolgamott Posted February 16, 2010 Share Posted February 16, 2010 Use isset to see if it ever was checked, will never appear in the post array (or get array if u want to use get instead) if never checked. So just a simple if statement below for all you check boxes to see if any was checked for each checkbox variable. Use the if statement to check then do the your script to deal with the checkboxes later in an else statement if they are. if(!isset($_POST['interest'])) echo "The checkbox intererst isn't checked"; Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013094 Share on other sites More sharing options...
doforumda Posted February 16, 2010 Author Share Posted February 16, 2010 Use isset to see if it ever was checked, will never appear in the post array (or get array if u want to use get instead) if never checked. So just a simple if statement below for all you check boxes to see if any was checked for each checkbox variable. Use the if statement to check then do the your script to deal with the checkboxes later in an else statement if they are. if(!isset($_POST['interest'])) echo "The checkbox intererst isn't checked"; thanks it is working now the problem is lets say a loggedin user already has selected Men and Women in interest in. Now he wants to remove Women so he will uncheck then it should also remove it from db. how can we achieve this task??? Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013108 Share on other sites More sharing options...
gwolgamott Posted February 16, 2010 Share Posted February 16, 2010 Mmmm, would basically do the same thing as when you add the checked items to the db. But depending on how you are setup for example say you have an update page where you lists all the items that can be checked with the items in their database section prechecked. With that setup then when they hit update this way you just clear the data from the database and re-enter the data to the db as you did before but with the new set of checked boxes. Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013115 Share on other sites More sharing options...
doforumda Posted February 16, 2010 Author Share Posted February 16, 2010 Mmmm, would basically do the same thing as when you add the checked items to the db. But depending on how you are setup for example say you have an update page where you lists all the items that can be checked with the items in their database section prechecked. With that setup then when they hit update this way you just clear the data from the database and re-enter the data to the db as you did before but with the new set of checked boxes. ok then lets say if a user who has not prechecked in anyone of Men and Women and when he checks one of it or both or them then there update query i think wont work so this case what should we do? Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013125 Share on other sites More sharing options...
gwolgamott Posted February 16, 2010 Share Posted February 16, 2010 Good point, well when we create the update page. You'd basically look to see if there is a value of 'm' or 'w' to interest after you've pulled data from the db. If it is neither one of these values then leave both unchecked. If it is m then pre-check men if it is w then pre-check women. A nested three loop system follow me? Here's some loose sudo code... hopefully the logic makes sense... it may not haven't had coffee yet. $data_from_database_for_interests = FROM_DATA_BASE STUFF_HERE; If ( $data_from_database_for_interests == 'm') { do precheck for with a value of m } If ( $data_from_database_for_interests == 'w') { do precheck for with a value of w } If (( $data_from_database_for_interests != 'm' || $data_from_database_for_interests != 'w')) { do the interest without a prechecked one } Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013165 Share on other sites More sharing options...
gwolgamott Posted February 16, 2010 Share Posted February 16, 2010 ok then lets say if a user who has not prechecked in anyone of Men and Women and when he checks one of it or both or them then there update query i think wont work so this case what should we do? Crap um... for both, sorry forgot to answer that part, for both then just do the same thing except add an addition line to the sudo code that checks for the both. Question is though how are you handling if both are checked? I mean how are you recording the data into the database? it's subject on that... and changes some things too as to how we handle it when you pull it out of the database. Anyrate this can still just be deteremined by conditional cases, but this may be more efficient to change it over to loops instead so we don't get a dozen if statements too. Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013167 Share on other sites More sharing options...
doforumda Posted February 16, 2010 Author Share Posted February 16, 2010 ok then lets say if a user who has not prechecked in anyone of Men and Women and when he checks one of it or both or them then there update query i think wont work so this case what should we do? Crap um... for both, sorry forgot to answer that part, for both then just do the same thing except add an addition line to the sudo code that checks for the both. Question is though how are you handling if both are checked? I mean how are you recording the data into the database? it's subject on that... and changes some things too as to how we handle it when you pull it out of the database. Anyrate this can still just be deteremined by conditional cases, but this may be more efficient to change it over to loops instead so we don't get a dozen if statements too. i am practicing lets see what happens Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013220 Share on other sites More sharing options...
gwolgamott Posted February 16, 2010 Share Posted February 16, 2010 Cool, well that's why I go here. Have to watch my logic some days since this forums my distraction for practice as I"m trying to work on something else. Ha I'll try to help someone and find myself telling them to do it backwards accidentally because not concentrating. right? haha. Link to comment https://forums.phpfreaks.com/topic/192243-need-help-with-check-boxes/#findComment-1013226 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.