-1
您好我正在創建一個網站使用HTML,CSS,PHP,MySQL和JavaScript的一點點的jQuery。 此刻我有3個下拉框,動態顯示您的選項,例如,如果我點擊英語,然後下一個下拉框顯示英語的所有部分,如閱讀,然後下一個將涉及閱讀這是所有工作完美。
我想要做的是使用sql查詢中的每個下拉框的值來返回與這些下拉框相關的特定視頻。
我需要爲$ _Post使用什麼類型的數組,以及如何構建sql查詢以便我可以爲視頻提取所有信息。
這是我目前使用的下拉菜單中的代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select#type").attr("disabled","disabled");
$("select#category").change(function(){
$("select#type").attr("disabled","disabled");
$("select#type").html("<option>wait...</option>");
var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){
$("select#type").removeAttr("disabled");
$("select#type").html(data);
});
});
$("select#principle").attr("disabled","disabled");
$("select#type").change(function(){
$("select#principle").attr("disabled","disabled");
$("select#principle").html("<option>wait...</option>");
var id = $("select#type option:selected").attr('value');
$.post("select_principle.php", {id:id}, function(data){
$("select#principle").removeAttr("disabled");
$("select#principle").html(data);
});
});
$("form#select_form").submit(function(){
var cat = $("select#category option:selected").attr('value');
var type = $("select#type option:selected").attr('value');
var princ = $("select#principle option:selected").attr('value');
if(cat>0 && type>0 && princ>0)
{
var result = $("select#principle option:selected").html();
$("#result").html('your choice: '+result);
}
else
{
$("#result").html("you must choose two options!");
}
return false;
});
});
</script>
</head>
<body>
<?php include "select.class.php"; ?>
<form id="select_form">
Choose a category:<br />
<select id="category">
<?php echo $opt->ShowCategory(); ?>
</select>
<br /><br />
Choose a type:<br />
<select id="type">
<option value="%">any...</option>
</select>
<br /><br />
Choose a principle:<br />
<select id="principle">
<option value="%">any...</option>
</select>
<br /><br />
<input type="submit" value="confirm" />
</form>
<div id="result"></div>
</body>
</html>
這是運行SQL查詢
<?php
class SelectList
{
protected $conn;
public function __construct()
{
$this->DbConnect();
}
protected function DbConnect()
{
include "db_config.php";
$this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
mysql_select_db($db,$this->conn) OR die("can not select the database $db");
return TRUE;
}
public function ShowCategory()
{
$sql = "SELECT * FROM subject";
$res = mysql_query($sql,$this->conn);
$category = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value="' . $row['subject_id'] . '">' . $row['description'] . '</option>';
}
return $category;
}
public function ShowType()
{
$sql = "SELECT * FROM section WHERE subject_id=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$type = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$type .= '<option value="' . $row['section_id'] . '">' . $row['description'] . '</option>';
}
return $type;
}
public function ShowPrinciple()
{
$sql = "SELECT * FROM principle WHERE section_id=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$principle = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'] . '</option>';
}
return $principle;
}
}
$opt = new SelectList();
?>
爲什麼投票下來? – WhySoSerious
我猜有人投票失敗,因爲你沒有提供任何代碼來說明你的問題/問題。 – Brice
1)接受選項,並用'$ _POST'超全局數組訪問它們,2)轉義所有的用戶輸入,3)創建查詢,4)執行它。你究竟遇到了哪個部分? –