请帮帮我。。当我选择表单时,它显示"select_modelcar.php?brandid=undefined
",但我试图将此代码粘贴到url中并定义id"select_modelcar.php?brandid=40
",结果如下。我想以选择的形式选择相同的类别,比如当我选择"丰田"品牌时,它会显示丰田品牌的所有车型(凯美瑞、雅力士等)。
当我单击"选择表单">>
在此处输入图像描述
url中的过去并定义id>>
在此处输入图像描述
select_brandcar.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<?php $servername = "localhost";
$username = "root";
$password = "usbw";
mysql_connect($servername,$username,$password);
mysql_select_db("carspecth");
?>
<body>
<script>
function ValueID(){
document.getElementById("getval").innerHTML = ('select_modelcar.php?brandid='+this.value);;
};
</script>
<select name="select_brandcar" id="select_brandcar" onclick="ValueID();" >
<option>Press Choose</option>
<?php
$sql = sprintf ("SELECT * FROM brand" );
$res = mysql_query ($sql);
while ($arr = mysql_fetch_array($res)){
printf ("<option value='%s'>%s</option>" ,$arr['brandid'], $arr['brandname']);
}
?>
<span id="getval"></span>
</body>
</html>
select_modelcar.php
<?php
mysql_query("SET NAMES UTF8");
include 'include_connectdb.php';
@$varbrandid = $_GET['brandid'];
@$sql = sprintf("SELECT * FROM maingeneration WHERE brandfk = %s", $varbrandid);
/*id ของตาราง catagory*/
@$res = mysql_query($sql);
printf("<select name='select' id='select'>");
while ($arr = mysql_fetch_array($res)) {
printf("<option value='%s'>%s</option>", $arr['maingenerationid'], $arr['maingenerationname']);
}
printf("</select>");
?>
谢谢。。。。。。。。。。。!
Please use onchange event instead of onclick and pass the value in the ValueID function.
Please replace javascript and php code with this:
<script>
function ValueID(vid){
$.ajax({
url: 'select_modelcar.php?brandid='+vid,
type: 'POST',
success: function (data) {
if (data === 'success') {
document.getElementById("getval").innerHTML = data;
}
}
});
};
</script>
<select name="select_brandcar" id="select_brandcar" onchange="ValueID(this.value);" >
<option>Press Choose</option>
<?php
$sql = sprintf ("SELECT * FROM brand" );
$res = mysql_query ($sql);
while ($arr = mysql_fetch_array($res)){
printf ("<option value='%s'>%s</option>" ,$arr['id'], $arr['name']);
}
?>
<span id="getval"></span>
In PHP FIle:
<?php
mysql_query("SET NAMES UTF8");
include 'include_connectdb.php';
@$varbrandid = $_GET['brandid'];
@$sql = sprintf("SELECT * FROM maingeneration WHERE brandfk = %s", $varbrandid);
/*id ของตาราง catagory*/
@$res = mysql_query($sql);
$select = "<select name='select' id='select'>");
while ($arr = mysql_fetch_array($res)) {
$select .= printf("<option value='%s'>%s</option>", $arr['maingenerationid'], $arr['maingenerationname']);
}
$select .="</select>";
echo $select;
exit;
?>
试试这个:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<?php $servername = "localhost";
$username = "root";
$password = "usbw";
mysql_connect($servername,$username,$password);
mysql_select_db("carspecth");
?>
<body>
<script>
function ValueID(element){
document.getElementById("getval").innerHTML = ('select_modelcar.php?brandid='+element.value);;
};
</script>
<select name="select_brandcar" id="select_brandcar" onclick="ValueID(this);" >
<option>Press Choose</option>
<?php
$sql = sprintf ("SELECT * FROM brand" );
$res = mysql_query ($sql);
while ($arr = mysql_fetch_array($res)){
printf ("<option value='%s'>%s</option>" ,$arr['brandid'], $arr['brandname']);
}
?>
</select>
<span id="getval"></span>
</body>
</html>