[英]PHP getting selected table rows from checkboxes
我有一個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”的提交按鈕,正如我剛才所說的,我想使用它,只是選擇要確保其正常工作的內容,但實際上並沒有給我任何輸出,因此可以提供任何幫助不勝感激。
在確定是否選中復選框時,您有權使用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的類型限制比較。
您的行格式可能是問題
echo "<td><input type='checkbox' name='checkbox[$rownumber]' value='[$rownumber]' </td>";
更改為:
echo "<td><input type='checkbox' name='checkbox[" . $rownumber . "]' value='". $rownumber . "' </td>";
而且因為您是從0開始編號,所以也可以只使用checkbox[]
作為名稱。
這將有助於這種形式的生活。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.