我有两个组合框,一个是Category,另一个是City,根据我从组合框中选择的内容,我需要在单击提交按钮时导航到不同的页面。
例如:如果组合框中的所选项目是汽车,我需要导航到cars.html页面,如果所选项目是自行车,我需要在点击提交按钮时导航到bike.html页面。
这是我的代码的一部分,
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<div id="absolute">
<h1><b>Select a category & a City </b></h1>
<table class="spacingTable" width="600">
<tr>
<td><label name="category">Category</label></td>
<td>
<select id="cmb1" name="Category">
<option value="select">Please select a Category</option>
<option value="Cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
<option value="threewheelers">Three Wheelers</option>
<option value="van">Vans & Busses</option>
<option value="hvd">Heavy-Duty vehicles</option>
</select></td>
</tr>
<tr>
<td><label name="City">City</label></td>
<td>
<input type="text" id="textbox1" name="city">
</td>
<td>
<div id="div-ok">
<input type="submit" name="Submit" value="Ok">
</div></td>
</tr>
</table>
</div>
</form>
</body>
this is php code snippet
<?php
if(isset($_POST['Submit'])){
if(!empty($_POST['Category'])){
// redirect the user to cars.html or mbikes.html
header("Location: " . $_POST['Category'] . ".html");
exit();
}
}
?>
答案 0 :(得分:1)
简而言之,你可以这样做:
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>
<select name="category">
<option value="cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
</select>
</td>
<td>
<div id="div-ok">
<input type="submit" name="Submit" value="Ok">
</div>
</td>
</tr>
</table>
</form>
并处理这样的表格:
if(isset($_POST['Submit'])){
if(!empty($_POST['category'])){
// redirect the user to cars.html or mbikes.html
header("Location: " . $_POST['category'] . ".html");
exit();
}
}
以下是相关参考资料:
答案 1 :(得分:0)
$ _ SERVER [“PHP_SELF”]是表单的文件名,因此,当您提交表单时,它指向自己的页面。 为了使它工作,您必须将 Rajdeep Paul的 php代码放在表单文件的开头,而不是其他所有内容。 结果如下:
<?php
if(isset($_POST['Submit'])){
if(!empty($_POST['category'])){
// redirect the user to cars.html or mbikes.html
header("Location: " . $_POST['category'] . ".html");
exit();
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>
<select name="category">
<option value="cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
</select>
</td>
<td>
<div id="div-ok">
<input type="submit" name="Submit" value="Ok">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
我刚刚接受了 Rajdeep Paul的代码并对其进行了编辑,以便您更轻松。
答案 2 :(得分:0)
Javascript 解决方案:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="#">
<table>
<tr>
<td>
<select name="category" id="category">
<option value="cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
</select>
</td>
<td>
<div id="div-ok">
<input type="submit" value="Ok" id="goTo">
</div>
</td>
</tr>
</table>
</form>
<script>
document.getElementById('goTo').onclick = function(e){
e.preventDefault();
var page = document.getElementById('category').value,
extension = '.html';
window.location = category + extension;
}
</script>