【问题标题】:PHP getting selected table rows from checkboxesPHP从复选框中获取选定的表行
【发布时间】:2015-02-05 05:56:39
【问题描述】:

我有一个 PHP 表单,其中创建了一个包含服务器文件夹中所有文件的表,并且创建的每一行都分配了一个复选框,用于决定要删除或共享哪些文件等。每一行是给定一个名称checkbox[$rownumber] 来区分每一个,然后当单击一个按钮时,为了确保我可以让它工作,它只打印选定的行 - 但我不能让它工作。

最初,由于行是从服务器在表中创建的,这是代码,它可以按照我的意愿正确创建它们

<?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>

由此,我有一个名为“deleteFile”的提交按钮,正如我目前所说的那样,我想使用它,只需获取选择的内容以确保它有效,但它实际上并没有给我任何输出非常感谢您的帮助。

【问题讨论】:

  • var_dump($_REQUEST)isset() 检查之后的输出是什么?
  • 在 if(isset_REQUEST['deleteFile'])) 之后是数组 (4) { ["Search"]=> string(6) "Search" ["fileUpload"]=> string(0) "" ["deleteFile"]=> string(6) "删除" ["siteowner"]=> string(1) "1" }
  • 你能展示更多你的代码吗?完整的form.
  • 在@RST 上方添加了更多代码
  • COMPLETE form 怎么样。另外,您是否知道您正在使用两个table 标签?是故意的吗?

标签: php checkbox html-table


【解决方案1】:

您的行格式可能是问题

echo "&lt;td&gt;&lt;input type='checkbox' name='checkbox[$rownumber]' value='[$rownumber]' &lt;/td&gt;";

改成:

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

而且因为您是从 0 开始编号,所以您也可以只使用 checkbox[] 作为名称。

这将有助于这种形式的生活。

【讨论】:

    【解决方案2】:

    在确定是否选中复选框时使用isset 是正确的,但您使用的名称不同:

    isset($_POST['checkbox'])
    

    如果

    返回true
    <input type="checkbox" name="checkbox" />
    

    已检查。

    您必须使用 $_POST 数组中的名称来引用已发布的输入。


    在这种情况下,您需要检查checkbox[$rownumber],这看起来很简单,需要您在发布时知道行号:

    PHP 代码

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

    如果您不知道确切的输入名称,您可能需要遍历 $_POST 数组:

    PHP 代码

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

    注意 strpos 的类型严格比较。

    【讨论】:

    • $_REQUEST 中没有可参考的内容。
    • 我明白了,虽然之后也会出现名称差异问题。让我看看。
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 2018-08-07
    • 2016-06-28
    • 2013-09-06
    • 1970-01-01
    • 2021-10-07
    • 2017-04-18
    • 2014-04-05
    相关资源
    最近更新 更多