在发送到 PHP 之前从 HTML Select Tag 读取数据 [英] Read data from HTML Select Tag before sending to PHP
问题描述
我需要向 PHP 脚本发送一个选项,但用户只需点击一个类型链接,而不是点击提交按钮.
I need to send a option to a PHP script, but the instead of a user clicking on a submit button, they simply click a type link.
例如:
<select name="test">
<option value="1">1</option>
</select>
<a href="next.php">Next</a>
因此用户将选择 1,然后单击显示下一步"的链接.这几乎就像我需要在通过 GET 请求 (php.php?select=1) 将它发送到 php 之前阅读 select 标签.有什么想法可以克服这个障碍吗?
So the user will select 1, then click on the link that says "Next". It's almost like i need to read the select tag BEFORE sending it to the php by a GET request (php.php?select=1). Any ideas how I can pass over this roadblock?
推荐答案
You Have to use tag to make this possible or use ajax submit tu submit your form with
<form>
你可以这样做
You Have to use <form>
tag to make this possible or use ajax submit tu submit your form with <form>
you can do in this way
<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>
或者你也可以用提交:
<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>
正如您在问题中提到的,您想在提交到 php 之前获取 select 的值,您可以使用 jQuery ajax
在下面执行此操作
as you mentioned in question you want to get the value of select before submit to php you can use jQuery ajax
to do this look below
<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();
});
这篇关于在发送到 PHP 之前从 HTML Select Tag 读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!