我正在嘗試將 inex.php 檔案制作為儀表板頁面,該頁面顯示了作為 index.php 所在檔案夾的一部分的檔案的選擇。當用戶選擇檔案時,我希望它重定向到該頁面。為此,我使用 xammp 作為本地網路服務器。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
</head>
<body>
<h1>Admin portal</h1>
<h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>
<form action="" method="POST">
<label for="pages">page:</label>
<select name="pages" id="page">
<?php
foreach (glob("*.php") as $filename) {
echo "<option value='strtolower($filename)'>$filename</option>";
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
我正在使用 foreach 來獲取檔案夾中的所有檔案并將它們填充到選擇選單中。我想知道當我點擊提交時如何重定向到這些檔案。
uj5u.com熱心網友回復:
您的代碼是正確的,您只需從值中洗掉strtolower方法,并在選擇標簽上使用 onchange 。
編輯,提交表單時的第二個 Sol :
<form action="" method="POST" onsubmit="return submitForm()">
<label for="pages">page:</label>
<select name="pages" id="page" >
<?php
foreach (glob("*.php") as $filename) {
$filename = strtolower($filename);
echo "<option value='$filename'>$filename</option>";
}
?>
</select>
<br><br>
<input type="submit" id='submitBtn' value="Submit">
</form>
<script type="text/javascript">
function submitForm(){
window.location.href = document.getElementById("page").value;
return false;
}
</script>
第一個解決方案,試試看:
<select name="pages" id="page" onchange=" (window.location = this.value);">
<?php
foreach (glob("*.php") as $filename) {
$filename = strtolower($filename);
echo "<option value='$filename'>$filename</option>";
}
?>
</select>
uj5u.com熱心網友回復:
不要使用選擇來重定向,使用<a>標簽。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
</head>
<body>
<h1>Admin portal</h1>
<h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>
<h1>pages</h1>
<ul>
<?php
foreach (glob("*.php") as $filename) {
echo "<li><a href=" . strtolower($filename) . ">" . $filename . "</a></li>";
}
?>
</ul>
</body>
</html>
// 編輯:錯誤的連接運算元
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426619.html
