我需要向PHP脚本发送一个选项,但是用户只需单击一个类型链接而不是用户点击提交按钮。
例如:
<select name="test">
<option value="1">1</option>
</select>
<a href="next.php">Next</a>
因此用户将选择1,然后单击“下一步”的链接。这几乎就像我需要在通过GET请求(php.php?select = 1)将它发送到php之前读取选择标记。我有什么想法可以解决这个障碍吗?
答案 0 :(得分:3)
您必须使用<form>
代码才能实现此功能,或者使用ajax submit tu以<form action="next.php" method="POST">
<select name="test">
<option value="1">1</option>
</select>
<!-- Then after this don't use anchor tag <a> use input submit instead-->
<input type="submit" name="submit" value="Next"/>
</form>
提交您的表单,以此方式执行
<button>
或者您也可以使用<form action="next.php" method="POST">
<select name="test">
<option value="1">1</option>
</select>
<!-- Then after this don't use anchor tag <a> use button instead-->
<button type="submit">Next</button>
</form>
jQuery ajax
正如您所提到的那样,您希望在提交到php之前获取select的值,您可以使用<form id="my_form" action="">
<select id="my_tag" name="test">
<option value="1">1</option>
</select>
<button type="submit">Next</button>
</form>
来执行此操作
$('#my_form').submit(function(e){
if($('#my_tag').val() !== '') {
var select = $('#my_tag').val();
$.ajax({
type: 'Get', // Could be GET/POST
url: 'php.php?select='+select, //targeting url with value from select tag
success: function(data){
alert(data); // alerting the data which php file returned.
}
});
} else {
alert('error'); // if select tag is empty we will show an error.
}
e.preventDefault();
});
的Ajax:
enum State {
enable, disable, deleted;
}
class Test {
public static void main(String[] args) {
for ( State s : State.enable.disable.values() ) {
System.out.println(s);
}
}
}