2014-12-07 26 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