crunchie Posted April 26, 2012 Share Posted April 26, 2012 Hey guy, I have a form where I want to validate if the check boxes have been checked or not and store that in a session. If a box is not selected I want an error message to echo. With what I've done so far, if a box is selected the form works but if one is not selected my error message does not show and I get an undefined Index Notice. Can anyone tell me where I'm going wrong. Page 1- form <form action="processPage7.php" method="post"> <br /> <p>Check the times you are most likely to have available:</p> <input type="checkbox" name="days[]" value="Evenings" <?php if(isset($_POST['days']) == 'Evenings') echo 'checked' ?>> Weekday evenings<br /> <input type="checkbox" name="days[]" value="Weekdays" <?php if(isset($_POST['days']) == 'Weekdays') echo 'checked' ?>> Week Days<br /> <input type="checkbox" name="days[]" value="Weekends" <?php if(isset($_POST['days']) == 'Weekends') echo 'checked' ?>> Weekend Days<br /> </div><br /><br /> <p><input type="submit" value="Next Page" /></p><input type="reset" value="Reset" /> </form> Page 2- <?php session_start(); if($_SERVER['REQUEST_METHOD']== 'POST'){ $valid_form = TRUE; // NO PROBLEMS if(!isset($_POST['days'])){ echo "Error, no time selected. Please go back and select a time."; }else{ $_SESSION['days'] = $_POST['days']; } if($valid_form == TRUE){ header('Location: FormProcess.php'); } } ?> And Page 3 <?php session_start(); $days = $_SESSION['days']; $chkResults = $days; for ($i = 0;$i<=count($chkResults)-1;$i++){ echo "<p>". $days[$i]; } ?> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/261672-need-help-with-check-boxes-validation-and-sessions/ Share on other sites More sharing options...
teynon Posted April 27, 2012 Share Posted April 27, 2012 Which page are you getting the notice on? Also, please place your code in [code=php:0] or [code] blocks. Your most likely issue is this: if($_SERVER['REQUEST_METHOD']== 'POST'){ $valid_form = TRUE; // NO PROBLEMS if(!isset($_POST['days'])){ echo "Error, no time selected. Please go back and select a time."; }else{ $_SESSION['days'] = $_POST['days']; } if($valid_form == TRUE){ header('Location: FormProcess.php'); } You are setting a valid_form variable to true. Then, regardless of whether or not the POST variable is sent, redirecting to the form process page. You should be doing something like this: $valid_form = false; if (!empty($_POST['days'])) { $_SESSION['days'] = $_POST['days']; header("Location: FormProcess.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/261672-need-help-with-check-boxes-validation-and-sessions/#findComment-1340928 Share on other sites More sharing options...
crunchie Posted April 27, 2012 Author Share Posted April 27, 2012 Thanks very much Teynon, that is exactly where the problem was. So simple now that I see what was wrong. Quote Link to comment https://forums.phpfreaks.com/topic/261672-need-help-with-check-boxes-validation-and-sessions/#findComment-1340954 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.