a.html
function load(str)
{
xhttp.open("GET","a.php?q="+str,true);
xhttp.send();
}
}
<select name = "select" onchange ="load(this.value)">
<option selected = "selected">Select a movie</option>
<option value= "asc">Name in ascending order</option>
<option value = "genre">Genre</option>
</select>
a.php
$asc = $_POST['asc'];
$genre = $_POST['genre'];
if (!empty($_GET[$asc])) {
$sql = "SELECT * FROM movies order by Name ASC";
} else if (!empty($_GET[$genre])) {
$sql = "SELECT * FROM movies order by Genre ASC";
}
$db = mysql_connect("localhost","root","123");
$db_select = mysql_select_db('m',$db);
if ( ( $result = mysql_query( $sql, $db ) ) ) {
echo $result;
}
我想从下拉按钮中选择值。例如asc
,并将该值传递给a.php ($asc = $_POST['asc'])
。我怎么能那样做?
<html>
<head></head>
<body>
<select class="movie" name="select">
<option selected = "selected">Select a movie</option>
<option value= "asc">Name in ascending order</option>
<option value = "genre">Genre</option>
</select>
<div class="showMovie"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$('.movie').change(function(){
var movieType= $('.movie').val();
$.ajax({url:"a.php?movieType="+movieType,cache:false,success:function(result){
$(".showMovie").html(result);
}});
});
</script>
</body>
</html>
a.php
<?php
$movieType = $_GET['movieType'];
if ($movieType == "asc") {
$sql = "SELECT * FROM movies order by Name ASC";
} else if ($movieType == "genre") {
$sql = "SELECT * FROM movies order by Genre ASC";
}
$db = mysql_connect("localhost","root","123");
$db_select = mysql_select_db('m',$db);
if (($result = mysql_query($sql, $db))) {
while($movieName = mysql_fetch_array($result)) {
echo $movieName['Name']."<br>";
}
}
?>
使用JS函数,您可以通过"get"方法发送它。所以你有你的价值进入$_GET['q']
。。。
如果要执行POST请求,请使用表单或XmlHttp函数:
function load(obj)
{
var xmlhttp = new XMLHttpRequest();
xmlxttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
//called when xmlhttp event occurs, here when we receive the response
console.log(xmlhttp.responseText); //Print the response into the console
}
}
xmlhttp.open("POST", "a.php");
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); //we send it as form
xmlhttp.send(obj.value + "=" + encodeURICompopent(obj.innerHTML));
}
并调用load(this)而不是load(this.value)
在php中,要检查是否发送了值,只需使用isset:
if (isset($_POST['asc'])) //do something;
但我不喜欢这种发送值的方式,这是不推荐的:请求参数名称应该是静态的。。。。
a.html
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#select_name').on('change', function() {
var str = $(this).val();
$.ajax({
type: "POST",
url: "a.php",
data: { q:str},
success: function(theResponse) {
// Output from ajaxpage.php
alert(theResponse); // comment this
}
});
});
});
</script>
<select name = "select_name" id = "select_name">
<option selected = "selected">Select a movie</option>
<option value= "asc">Name in ascending order</option>
<option value = "genre">Genre</option>
</select>
a.php
<?php
$db = mysql_connect("localhost","root","123");
$db_select = mysql_select_db('m',$db);
$q = isset($_POST['q']) ? $_POST['q'] : '';
if ($q == 'asc')
{
$sql = "SELECT * FROM movies order by Name ASC";
}
else if ($q == 'genre')
{
$sql = "SELECT * FROM movies order by Genre ASC";
}
else
{
echo 'Nothing';exit();
}
$qry = mysql_query($sql);
if (mysql_num_rows($qry) > 0)
{
$result = mysql_fetch_object($sql) ;
echo $result;
}
?>