0

I have a php file which lists all the files in a directory with checkboxes next to each one:

<html
<body>
<P>List of files:</p>

<form action="submitfiles.php" method="post">

<?php
 if ($handle = opendir('./files')) {

   while (false !== ($file = readdir($handle)))
   {
          if ($file != "." && $file != "..")
          {
            $thelist .= '<a href="'.'./files/'.$file.'">'.$file.'</a>';
            $thelist .= '<br>';

            $s = '<input type="checkbox" name="'.$file.'" value="yes"/><a href="'.'./files/'.$file.'">'.$file.'</a>';
            echo $s;
            echo '<br';
          }
   }
  closedir($handle);
  }
?>
</body>

<input type="submit" name="formSubmit" value="Submit"/>
</form>

</html>

Then, I have a submitfiles.php, which loops through all the files, and gets the $_POST values for each checkbox.

<?php


 if ($handle = opendir('./files')) {
   while (false !== ($file = readdir($handle)))
   {
          if ($file != "." && $file != "..")
          {

                echo 'Value is ' . $_POST['$file'] . '<br>';
                echo $file . '<br>';

          }
   }
  closedir($handle);
  }

?>

Even if I check a checkbox though, the line "Value is " in my php file always prints a blank. Meaning, none of the checkboxes are ever given a value. How do I get this working? If I checked a checkbox, I want it to print "Value is yes", but it doesn't.

2
  • have you tried doing a var_dump of POST to see what you're actually receiving as input to submitfiles.php? Are you sure that $file is valid as a name attribute? Commented Feb 28, 2011 at 17:24
  • Your body and form are entangled. It is <body><form>...</body></form> here. Commented Aug 25, 2013 at 16:38

4 Answers 4

4

Okay - there is a slight disconnect in your code...

Here is the checkbox:

<input type="checkbox" name="'.$file.'" value="yes"/>

Here is your attempt to get it:

$_POST['$file']

The name of the checkbox isn't "$file" - it is the value of the variable $file...

So try changing them to...

<input type="checkbox" name="file[]" value="yes"/>

And get them like this...

$FileCheckBoxes = $_POST['file'];

Now $FileCheckBoxes will contain a list of the ones that were checked.

Sign up to request clarification or add additional context in comments.

Comments

0

You want to have checkboxes to output an array.

<input type="checkbox" name="assigned_name[]" value="1"> Item 1
<input type="checkbox" name="assigned_name[]" value="2"> Item 2

Comments

0

On the line

echo 'Value is ' . $_POST['$file'] . '<br>';

in submitfiles.php, try not using quotes around $file, like so:

echo 'Value is ' . $_POST[$file] . '<br>';

Using single quotes forces PHP to not evaluate the value of $file. Read up on single quotes at http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

Comments

0

The problem is this line here:

echo 'Value is ' . $_POST['$file'] . '<br>';

Remove the single quotes around the array key (or use double quotes):

echo 'Value is ' . $_POST[$file] . '<br>';

When you use single quotes, the variable $file is being passed as a string. You can read more about why this is true here: http://php.net/string/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.