PHP从复选框获取选定的表格行 [英] PHP getting selected table rows from checkboxes

查看:93
本文介绍了PHP从复选框获取选定的表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php表格,其中创建一个文件夹中的所有文件的服务器上的表,创建的每一行分配一个复选框,将用于决定哪些文件将被删除或共享等。每行给定一个名称复选框[$ rownumber]来区分每一个,然后当一个按钮被点击时,为了确保我可以得到它的工作,它只打印所选择的行 - 但我不能得到这个工作。 / p>

最初,当从服务器在表中创建行时,这是代码,可以正确创建它们

 <?php 
if(isset($ _ REQUEST ['deleteFile']))
{var_dump($ _ REQUEST);
if(isset($ _ POST ['checkbox']))
{

print_r($ _ POST);
}
}
?>

< form name =mainHomescreenaction =MainHomescreen.phpmethod =POST>
< nav>
< input type =textname =Searchvalue =Searchstyle =color:#888; onFocus =inputFocus(this)onBlur =inputBlur(this)/>
< / nav>

< sidebar>
< input type =buttonvalue =创建文件夹onClick =createFolder()/>
< input type =buttonvalue =下载所选文件/文件夹onClick =/>
< input type =buttonvalue =加密所选项onClick =encrypt()/>< br>
< input type =buttonvalue =分享所选项onClick =shareFolder()/>

<! - 上传文件 - >
< div id =fileUpload>
< div id =uploadPopup>
< form action =<?php echo $ _SERVER ['PHP_SELF'];?> id =uploadFormmethod =postname =uploadFormenctype =multipart / form-data>

选择要上传的文件:< input name =fileUploadtype =file/>< br />
< input type =submitvalue =上传文件name =submitFile/>
< / form>
< / div>
< / div>
< input type =buttonname =uploadvalue =上传文件onClick =showUpload()/>
< input type =submitvalue =Deletename =deleteFile/>
< input type =buttonvalue =RenameonClick =/>< br>
< input type =buttonvalue =密码保护文件夹/文件onClick =/>< br>< br>
< / sidebar>
< article>
< span class =error><?php echo $ error;?>< / span>
< table id =detailViewstyle =width:100%>
<! - php创建表 - >
<?php
//打开文件名
$ dir = opendir(/ home / a1962403 / public_html / files);

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

echo'< table cellpadding =10cellspacing =0style =width:100%>';
echo'
< tr>
< th> < / th>
< th> File< / th>
< th>日期< / th>
< th>大小< / th>
< / tr>
';

$ rownumber = 0;

//在目录中列出文件
while(($ file = readdir($ dir))!== false)
{
//获取文件日期,字符串需要解码否则抛出错误。

$ 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;

//打印表行
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>';
closingir($ dir);
?>
< / article>
< / form>

< / body>
< / html>

从这里,我有一个名为deleteFile的提交按钮,我想使用我现在说,只是得到什么是选择,以确保它的工作,但它实际上并没有给我任何输出,所以任何帮助将非常感激。



谢谢。 / p>

解决方案

您的行格式可能是问题



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



更改为:

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

因为你的编号从0开始,你也可以使用 ] 作为名称。



这将有助于此表单的生效。


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.

Thanks.

解决方案

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.

这篇关于PHP从复选框获取选定的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆