Jump to content

Need help with check boxes, validation and sessions


crunchie

Recommended Posts

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.

Link to comment
Share on other sites

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");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.