2014-12-07 64 views
0

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

首先,与行从服务器表中创建的,这是代码,它正确地创建它们,因为我希望

<?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”,这正如我刚才所说的那样,我想要使用它,只是为了确保它能够正常工作而获得所选内容,但实际上并没有给我任何输出,因此不胜感激。

+0

'isset()'检查后'var_dump($ _ REQUEST)'的输出是什么? – RST 2014-12-07 14:11:05

+0

if(isset_REQUEST ['deleteFile']))它是数组(4){[“Search”] => string(6)“Search”[“fileUpload”] => string(0)“”[“deleteFile”] => string(6)“Delete”[“siteowner”] => string(1)“1”} – Navvy 2014-12-07 14:13:37

+0

你能展示更多的代码吗?完整的“表格”。 – RST 2014-12-07 14:27:44

回答

0

你行格式可能是问题

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

将其更改为:

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

而且因为你是从0开始编号了你也可以只使用checkbox[]作为名字。

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

+0

可爱的这终于得到了输出,谢谢。 – Navvy 2014-12-08 08:33:28

0

你是正确的,确定一个复选框是否被选中与否时使用isset,但使用的是differring名称:如果

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

检查

isset($_POST['checkbox']) 

将返回true。

您必须使用$_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类型-严格比较。

+0

那样在'$ _REQUEST'中没有任何内容可以引用。 – RST 2014-12-07 14:54:57

+0

我明白了,但名称差异问题在此之后也会出现。让我看看。 – 2014-12-07 14:58:34