我有一个类似的表单,我想根据表单字段的选定值
更改操作希望行动成为
http://www.somesite.com/index.php?action=test
或
http://www.somesite.com/index.php?action=test1
<form action="http://www.somesite.com/index.php?action=<?php echo $Selected?>" method="post">
<label for="select">Select:</label>
<select name="select" id="select">
<option value="test">TEST</option>
<option value="test1">TEST1</option>
</select>
<input type="Submit" name="submit" value="submit" />
</form>
答案 0 :(得分:0)
非常简单的代码,php是服务器端,在发送给用户后无法更改客户端html操作,因此我们必须检查选择:
<?php
if (isset($_POST['select'])) {
if ($_POST['select'] === "test") {
header("Location: http://www.somesite.com/index.php?action=test");
} else if ($_POST['select'] === "test1") {
header("Location: http://www.somesite.com/index.php?action=test1");
}
}
?>
<form action=<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="select">Select:</label>
<select name="select" id="select" value=>
<option value="test">TEST</option>
<option value="test1">TEST1</option>
</select>
<input type="Submit" name="submit" value="submit" />
</form>
答案 1 :(得分:0)
您可以使用onchange事件,并更改参数。会是这样的:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#select').change(function() {
var param_val = $(this).val();
var param = "action"; //param in url we want to change
var href = $('#form1').attr('action');
var regex = new RegExp("([?;&])" + param + "[^&;]*[;&]?")
var query = href.replace(regex, "$1").replace(/&$/, '')
var href = (query.length > 2 ? query + "&" : "?") + param + "=" + param_val
$('#form1').attr('action', href);
});
});
</script>
<form id="form1" action="http://www.somesite.com/index.php?action=test" method="post">
<label for="select">Select:</label>
<select name="select" id="select">
<option value="test">TEST</option>
<option value="test1">TEST1</option>
</select>
<input type="Submit" name="submit" value="submit" />
</form>