Javascript PHP从下拉列表中获取文本
我有一个显示目录的下拉列表。如果我选择一个并按下Submit按钮,将执行一个操作。现在我需要第二个按钮下载,它压缩文件并下载zip文件。 我的问题是,我无法在PHP中获取所选项目。 这是我的密码:Javascript PHP从下拉列表中获取文本,javascript,php,html,Javascript,Php,Html,我有一个显示目录的下拉列表。如果我选择一个并按下Submit按钮,将执行一个操作。现在我需要第二个按钮下载,它压缩文件并下载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/1
<!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>
选择一个文件夹
功能测试(){
var e=document.getElementById(“myDirs”);
log(e.options[e.selectedIndex].value);
}
名为myDirs的select元素不在表单元素中。只有当select元素位于表单元素内部时,它的数据才会与表单一起发布。您选择的元素不在表单内部
你应该使用
$dirs=$\u POST['myDirs']代码>而不是
$dirs=$\u GET['myDirs']代码>
当表单方法设置为POST
时,首先将选择
放入表单中,然后更改
$dirs = $_GET['myDirs'];
$dirs = $_REQUEST['myDirs'];
到
因为您的表单操作是POST
$dirs = $_REQUEST['myDirs'];
使用$\u请求,然后您将获取并发布数据