newbees Posted September 9, 2015 Share Posted September 9, 2015 Hi Sir, I creating a form with checkbox for example I checked the Add Employee and Upload Employee, after I click the Submit button the output is 2 and 3 which is correct but only the Upload Employee was checked. How can it be that the Add Employee and Upload Employee was checked. Also how can save checkbox check in the database. if(isset($_POST['submit'])){ if(!empty($_POST['access'])) { $access = $_POST['access']; echo "You chose the following color(s): <br>"; foreach ($access as $access_id){ echo $access_id."<br />"; }} // end brace for if(isset else { echo "You did not choose a color."; }}<input type="checkbox" name="access[]" id="access" value="1" <?php if($access_id==1) echo 'checked="checked"'; ?>>Search/View Employee Information<br><input type="checkbox" name="access[]" id="access" value="2" <?php if($access_id==2) echo 'checked="checked"'; ?>>Add Employee<br><input type="checkbox" name="access[]" id="access" value="3" <?php if($access_id==3) echo 'checked="checked"'; ?>>Upload Employee<br><input type="checkbox" name="access[]" id="access" value="4" <?php if($access_id==4) echo 'checked="checked"'; ?>>Permission<br><input type="checkbox" name="access[]" id="access" value="5" <?php if($access_id==5) echo 'checked="checked"'; ?>>Vacation<br><input type="checkbox" name="access[]" id="access" value="6" <?php if($access_id==6) echo 'checked="checked"'; ?>>Calendar Event<br> <input type="submit" name="submit" id="submit" value="Submit"> Thank youHi Sir,I creating a form with checkboxfor example I checked the Add Employee and Upload Employee, after I click the Submit button the output is 2 and 3 which is correct but only the Upload Employee was checked.How can it be that the Add Employee and Upload Employee was checked.Also how can save checkbox check in the database. if(isset($_POST['submit'])){ if(!empty($_POST['access'])) { $access = $_POST['access']; echo "You chose the following color(s): <br>"; foreach ($access as $access_id){ echo $access_id."<br />"; }} // end brace for if(isset else { echo "You did not choose a color."; }}<input type="checkbox" name="access[]" id="access" value="1" <?php if($access_id==1) echo 'checked="checked"'; ?>>Search/View Employee Information<br><input type="checkbox" name="access[]" id="access" value="2" <?php if($access_id==2) echo 'checked="checked"'; ?>>Add Employee<br><input type="checkbox" name="access[]" id="access" value="3" <?php if($access_id==3) echo 'checked="checked"'; ?>>Upload Employee<br><input type="checkbox" name="access[]" id="access" value="4" <?php if($access_id==4) echo 'checked="checked"'; ?>>Permission<br><input type="checkbox" name="access[]" id="access" value="5" <?php if($access_id==5) echo 'checked="checked"'; ?>>Vacation<br><input type="checkbox" name="access[]" id="access" value="6" <?php if($access_id==6) echo 'checked="checked"'; ?>>Calendar Event<br> <input type="submit" name="submit" id="submit" value="Submit"> Thank you Link to comment Share on other sites More sharing options...
dsonesuk Posted September 9, 2015 Share Posted September 9, 2015 Because the checkbox if conditions are not part of foreach loop, when the foreach loop finishes the $access_id variable would be last value retrieved from $_POST['access'] , then the checkbox inputs would be rendered and only the checkbox if condition that matched the last value produced by foreach loop would be checked. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now