Jump to content
Larry Ullman's Book Forums

Check Box Validation And Its Value Display In Second Form


Recommended Posts

Hello..

 

I have Two HTML form and one is a Check boxes form that enable users to select their category. Then I need to display second form according to the categories they selected in first form. I use this code in first form to validate form submission..

 

if ( isset( $_POST['category']) && sizeof( $_POST['category']) <= 3) {

 

$category = $_POST['category'];

 

} else {

 

$errors[] = 'Please select atleast 1, not more than 3 categories';

}

 

If errors array is empty I did this..

 

if ( empty( $errors )) { // If everything's OK

 

$_SESSION = $category;

 

$url = 'http://localhost/lanka_institute/tutorsignup/select_subjects.php? // Define the URL.

ob_end_clean(); // Delete the buffer.

header("Location: $url");

exit(); // Quit the script.

 

}

 

 

Can I know and is this correct? I display category list from mysql category table, it has category name and category id

 

this is my html part from first page

 

echo '<td width="50%"><input type="checkbox" name="category[]" vlaue="' . $info['category_id'] . '" />  ' . $info['category_name'] . '</td>';

 

 

any help appreciated.

 

Thanks in advance..

Link to comment
Share on other sites

Well: Does it work?

 

I would've split the next page into a seperate file, then figure out which forms to include/load there. You also need some kind of flag to prevent the user from reaching step2.php before done with step1.php.

 


// session_start();

// include header
//......

// Set step in process
$_SESSION['step'] = 2;

if ( isset($_POST['categories']) && $_SESSION['step'] == 2 )
{
  $forms = $_POST['categories'];

  // Included wanted forms
  // If this is not allowed on server, use a switch/if-else
  foreach ( $forms as $key => $value )
  {
  include('form'.$key.'.php');
  }

}

// include footer

 

Just some thoughts. Might not be the best solution, but you could make that work. Set the $_SESSION['step'] to indicate the process or something. When step 1 is "valid", send the form to step2.php. You might need to store the checkbuttons in a session var instead of post, but bet you can work that out.

  • Upvote 1
Link to comment
Share on other sites

Thanks Antonio Conte,

 

But I couldn't understand exactly what have u said.

 

can you tell me how this code work and why??

 

  // Included wanted forms
  // If this is not allowed on server, use a switch/if-else
  foreach ( $forms as $key => $value )
  {
	  include('form'.$key.'.php');
  }

Link to comment
Share on other sites

Its still problem.. my first form is 'sign_up.php' and second is select_subject.php'. sign_up.php page has more categories and users can select up to 3 more categories there. So. after selecting and user click the continue bottom, page want to go to second form page its select_subject.php page. In there, i want to display the categories which user selected from sign_up.php page and also its provide to select subjects according to the categories they have selected.

 

my problem is how can I check what are the categories user have selected and how they display in my second page.

 

category come to first page from my category table. It has category_id and category_name columns.

 

thanks for any help..

Link to comment
Share on other sites

Just check the category_id then. You probably have something like this in your html:

 

<form action="select_subject.php" method="post">
<p>Select bla bla_</p>
<div>This <input type="checkbox" name="category[]" value="magazine"  /></div>
<div>That <input type="checkbox" name="category[]" value="t-shirt" /></div>
<div>Other <input type="checkbox" name="category[]" value="football"  /></div>
<div><button type="submit">Submit</button>
</form>

 

Try placing this in select_subject.php:

 

echo '<pre>' , print_r($_POST['category']) , '</pre>';

 

As you can see, it's an array when you add the brackets to name="category[]" in the form. Without them, you will only get one value. Let's say you press each one, this would be the $_POST['category'] array:

 

array(
  [0] => "magazine",
  [1] => "t-shirt",
  [2] => "football"
)

 

 

You can check if these values exist to add forms.

 

In select_subject.php:

 

$category_array = $_POST['category'];

// Flip value and keys for the check
$flipped= array_flip($category_array);

// Set flags for form to include
$magazine = false;
$t-shirt = false;
$football = false;

if ( array_key_exists('football', $flipped) )
{
   $magazine = true;
}
if ( array_key_exists('magazine', $flipped) )
{
   $t-shirt = true;
}
if ( array_key_exists('t-shirt', $flipped) )
{
   $football = true;
}
?>
// Maybe you start your html here:

// <head><body>, etc....

// The normal form
<form ....>

</form>

// Then test the flags

if ( $football) {
  include("football.php"); // Or echo it, whatever.
}

// You do the same for the others

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...