Page 1 of 1

Checkboxes if then else include

Posted: Wed Apr 27, 2011 11:42 am
by Ramm
Hello,

Im trying to make in PHP checkboxes that will include 1, 2 or 3 files when selected:

Index.php:

Code: Select all

<form action="checkbox-form.php" method="post"> 
Select the options:<br />
<input type="checkbox" name="formDoor[]" value="A" />First option<br />
<input type="checkbox" name="formDoor[]" value="B" />Second Option<br />
<input type="checkbox" name="formDoor[]" value="C" />Third Option<br />
<input type="submit" name="formSubmit" value="GENERATE" />
</form>
checkbox-form.php:

Code: Select all

<?php
  $aDoor = $_POST['formDoor'];
  if(empty($aDoor))
  {
    echo("You didn't select any options.");
  }
  else
  {
    echo("
    if $aDoor == A
    then #include A.php
    ");
?>
Basicly what I want is to include 3 files on the index.php.

When option A is selected A.php is included, when B is selected B.php is included etc.

I also want when multiple options are included: like A and C, it includes both A.php and C.php.

Any ideas how I can figure this out?

Re: Checkboxes if then else include

Posted: Wed Apr 27, 2011 1:14 pm
by McInfo
This could trigger an undefined index notice because it attempts to read an array element that might not exist:

Code: Select all

$aDoor = $_POST['formDoor'];
if (empty($aDoor))
However, empty and isset have the special ability of testing an undefined array element without triggering the notice. This is correct:

Code: Select all

if (empty($_POST['formDoor']))
To solve your problem, first create an array of valid include names. By comparing against that white-list, you can confirm that the values users submit are valid and prevent users from including files they shouldn't include.

The user input "formDoor" is expected to be an array. Therefore, you should test that it is an array. Your form is not the only way a user can access your processing script, so you should not assume anything about the input.

If all the checks so far have passed, loop through the "formDoor" array and test that each element exists in the array of white-listed values. If an element is in the white-list, include the associated file. Unless the submitted values are complete file names, you will have to build the paths using some of PHP's string manipulation techniques.