我想使用用户选择的选项值而不提交表单。这是我的HTML代码
Combo Box<br>
<select name="select1">
<option value="IVP" selected>IVP</option>
<option value="SURTEK">SURTEK</option>
<option value="GUITARTUNER">GUITARTUNER</option>
<option value="OTHER">OTHER</option>
</select>
我想把选择的选项值到一个php变量,以便根据选项值可以显示一组新的数据。由于
按照其他人的建议,您应该使用AJAX。我建议查看javascript/Jquery的AJAX调用示例。
我猜你想修改网页的一部分取决于用户选择的选项,最好的方法是有一个单独的PHP脚本接收选择的选项(捕获在javascript/JQuery),并返回新的HTML你想要显示
在Jquery中获取选中的选项:var selected_option_value=$("#select1 option:selected").val();
也在Jquery中做一个AJAX调用,使用POST传递值:
$.post("script_that_receives_value.php", {option_value: selected_option_value},
function(data){ //this will be executed once the `script_that_receives_value.php` ends its execution, `data` contains everything said script echoed.
$("#place_where_you_want_the_new_html").html(data);
}
);
希望这对你有帮助!
编辑:让我们给上面的例子更多的细节:
假设你有一个index.html页面,上面有问题中给出的<select name="select1">
:
第一步将是当有人选择其中一个选项时链接一个事件,如何做到这一点:
1-第一种方法:
<select name='select1' id='select1' onchange='load_new_content()'>
这样,当有人改变<select>
列表的选择值时,javascript函数load_new_content()
将被执行。注意我已经添加了id='select1'
的标签,这是用来搜索这个元素在javascript/JQuery,你应该总是使用id属性,如果你需要在javascript/JQuery使用该标签。
2-第二种方法,使用JQuery链接事件:
要做到这一点,你应该在index.html的<head>
中有一个<script>
标签。在这个<script>
标签中,你应该有:
$(document).ready(function(){
// everything here will be executed once index.html has finished loading, so at the start when the user is yet to do anything.
$("#select1").change(load_new_content()); //this translates to: "when the element with id='select1' changes its value execute load_new_content() function"
});
无论您想使用哪个选项,现在都需要这个load_new_content()
函数。它也应该在<head>
标记的<script>
标记中声明,就像$(document)一样。准备功能。
function load_new_content(){
var selected_option_value=$("#select1 option:selected").val(); //get the value of the current selected option.
$.post("script_that_receives_value.php", {option_value: selected_option_value},
function(data){ //this will be executed once the `script_that_receives_value.php` ends its execution, `data` contains everything said script echoed.
$("#place_where_you_want_the_new_html").html(data);
alert(data); //just to see what it returns
}
);
}
现在只剩下script_that_receives_value.php
:
<?php
$selected_option=$_POST['option_value'];
//Do what you need with this value, then echo what you want to be returned.
echo "you have selected the option with value=$selected_option";
?>
使用AJAX,同时在下拉菜单中选择一个选项触发jQuery函数并通过AJAX将值发送到服务器页面
HTML页面:
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
PHP页面:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
裁判:http://www.w3schools.com/php/php_ajax_database.asp
如果不发出http请求,就不能在php变量中获取选项值,因为所选值位于客户端,而php -位于服务器。
您可以使用ajax,但它仍然提交表单。如果您不想对服务器执行查询,则必须在客户端加载所有数据并使用JavaScript对其进行管理
如果您正在寻找动态web内容,Ajax是最好的选择