我有一个显示目录的下拉列表。如果我选择一个并按下按钮提交,将执行一个操作。现在我需要第二个按钮下载,它压缩文件并下载zip文件。 我的问题是我不会在PHP中获得所选的项目。 这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<select name="myDirs" id="myDirs">
<option value="0" selected="selected">Select a folder</option>
<?php
foreach(glob('auswertung/*', GLOB_ONLYDIR) as $dir) {
$dir = str_replace('auswertung/', '', $dir);
echo '<option value="'.$dir.'">'.$dir."</option>\n";
}
?>
</select>
<input type="submit" name="submit" onclick="test()" />
<form method="POST" action=''>
<input type="submit" value="Download Files" name="download">
</form>
<script type="text/javascript">
function test(){
var e = document.getElementById("myDirs");
console.log(e.options[e.selectedIndex].value);
}
</script>
<?php
if (isset($_POST['download'])) {
$dirs = $_GET['myDirs'];
$files = array('auswertung/"'.$dirs.'"/Coordinates.txt', 'auswertung/"'.$dirs.'"/MagnetField.txt', 'auswertung/"'.$dirs.'"/Timestamps.txt');
/*$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);*/
}
?>
</body>
</html>
我是PHP的新手。我收到以下错误消息:
注意:未定义的索引:第20行的C:\ PATH \ htdocs \ folder.php中的myDirs
我做错了什么?
答案 0 :(得分:1)
您选择的元素不在表单内。
你应该使用
$dirs = $_POST['myDirs'];
代替
$dirs = $_GET['myDirs'];
因为表单方法设置为POST
答案 1 :(得分:1)
$dirs = $_REQUEST['myDirs'];
使用$ _REQUEST然后你会得到&amp;发布数据
答案 2 :(得分:0)
名称为myDirs的select元素不在表单元素中。只有当select元素位于表单元素内部时,才会使用表单发布数据。
答案 3 :(得分:0)
首先将select
放入form
并更改
$dirs = $_GET['myDirs'];
到
$dirs = $_POST['myDirs'];
因为您的表单操作是POST