PHP从复选框中获取选定的表行

I have a PHP form where a table is created of all files in a folder on the server, and each row created is assigned a checkbox which will be used to decide which files are to be deleted or shared etc. Each row is given a name checkbox[$rownumber] to distinguish each, and then when a button is clicked, for the moment to ensure I can get it working, it just prints the rows that are selected - but I can't get this working.

Initially, as rows are created in the table from the server, this is the code, which correctly creates them as I wish

<?php
if(isset($_REQUEST['deleteFile']))  
{var_dump($_REQUEST);
    if(isset($_POST['checkbox']))
    {

        print_r($_POST);
    }
}
?>

<form name="mainHomescreen" action="MainHomescreen.php" method="POST">
<nav>
    <input type="text" name="Search" value="Search" style = "color:#888;" onFocus="inputFocus(this)" onBlur="inputBlur(this)"/>
</nav>

<sidebar>
<input type="button" value="Create Folder" onClick="createFolder()"/>
<input type="button" value="Download Selected Files/Folders" onClick=" "/>
<input type="button" value="Encrypt Selected" onClick="encrypt()"/><br>
<input type="button" value="Share Selected" onClick="shareFolder()"/>

<!-- upload a file -->
<div id="fileUpload">
    <div id="uploadPopup">
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="uploadForm" method="post" name="uploadForm" enctype="multipart/form-data">

        Select file to upload: <input name="fileUpload" type="file" /><br />
        <input type="submit" value="Upload File" name="submitFile"/>
    </form>
    </div>
</div>
<input type="button" name="upload" value="Upload File" onClick="showUpload()"/>
<input type="submit" value="Delete" name="deleteFile"/>
<input type="button" value="Rename" onClick=" "/><br>
<input type="button" value="Password Protect Folder/ File" onClick=" "/><br><br>
</sidebar>
<article>
<span class="error"><?php echo $error;?></span>
<table id="detailView" style="width:100%">
<!-- php to create table -->
<?php
//open file names
$dir = opendir("/home/a1962403/public_html/files");

$filearray = array(array(array()));

echo '<table cellpadding="10" cellspacing="0" style="width:100%">';
echo '
<tr>
    <th> </th>
    <th>File</th>
    <th>Date</th>
    <th>Size</th>
</tr>
';

$rownumber = 0;

  //List files in directory
  while (($file = readdir($dir)) !== false)
   {
    //gets the file date, string needed decoding otherwise throws error.

    $date = @filemtime($file);
    $date = date("F d Y H:i:s.", filemtime(utf8_decode("/home/a1962403/public_html/files/$file")));

    $size = filesize("/home/a1962403/public_html/files/$file") . ' bytes';

    $rownumber = $rownumber + 1;

    //prints a table row
    echo '<tr class="bottomline">';
    echo "<td><input type='checkbox' name='checkbox[$rownumber]' value='[$rownumber]' </td>";
    echo "<td>$file</td>";
    echo "<td>$date</td>";
    echo "<td>$size</td>";
    echo '</tr>';
}
echo '</table>';
closedir($dir);
?>
</article>
</form>

</body>
</html>

From this, I have a submit button called 'deleteFile', which I am wanting to use to as I said for the moment, just get what is selected to ensure it works, but it's not actually giving me any output so any help would be greatly appreciated.

Your row formatting may be the problem

echo "<td><input type='checkbox' name='checkbox[$rownumber]' value='[$rownumber]' </td>";

Change it to:

   echo "<td><input type='checkbox' name='checkbox[" . $rownumber . "]' value='". $rownumber . "' </td>";

And because your are numbering from 0 up you could also just use checkbox[] as a name.

It would help to this form live.

You are right to use isset when determining whether a checkbox is checked or not, but you are using differring names:

isset($_POST['checkbox'])

will return true if

<input type="checkbox" name="checkbox" />

is checked.

You have to refer to posted inputs using their names in the $_POST array.


In this case, you need to check for checkbox[$rownumber], which, as is seemingly trivial, requires you to know the row numbers when posting:

PHP code

if (isset($_POST["checkbox[$rownumber]"]))
{
    // This checkbox is checked
}

If you don't know the exact input names, you might want to iterate through the $_POST array:

PHP code

foreach ($_POST as $input_name => $input_value)
{
    if (strpos($input_name, 'checkbox') !== false)
    {
        // This input's name contains the substring 'checkbox'
    }
}

Note the type-strict comparison at strpos.