我想将我的选择值作为我的表单操作+字符串(/index.php) 但是在我点击提交按钮之前,我似乎无法获得选择的值。
<tr>
<td>Select Project: </td><td><select name="myproject" onchange="">
<option>Exam1</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" id="login" value="Submit"
formaction="<?php $myproject = $_POST['myproject']; echo $myproject."/index.php"; ?>"
formmethod="POST"/>
</td>
</tr>
答案 0 :(得分:2)
这是正确的。您正在混合服务器端和客户端。
阅读你的评论后,我相信你想要这个(普通的JS选择)
<script>
window.onload=function() {
document.getElementsByName("myproject")[0].onchange=function() {
var path = this.value;
if (path) this.form.action=path+'/index.php';
}
}
</script>
现在使用HTML:
<form method="post" action="">
Select Project: <select name="myproject">
<option value="">Please select</option>
<option value="exam1">Exam1</option>
</select>
<input type="submit">
</form>
要在服务器上执行此操作,请查看Post to another page within a PHP script
答案 1 :(得分:0)
$myproject = !empty($_POST['myproject']) ? $_POST['myproject'] : "defaultaction.php";