[英]How php, javascript and mysql to one combo, if clicked comes in another through another query
我試圖通過這個論壇有很多不同的帖子來解決問題。 但是無法達到我的特定目標。
我正在嘗試使數據從表中的列到組合,然后單擊任何一個數據,它應該根據單擊的數據從另一個表中的另一列列出到另一個組合。
請檢查我的網站:raihans.co.uk,用戶:dbuser,passwd。 然后將鼠標懸停在用戶登錄/退出上,單擊“數據庫測試”。
作為示例,您可能會看到在頂部創建的組合。 在底部,我嘗試使用mysql,javascript和php進行測試。 從我的一張桌子成功地填充了我在第一組合中的第一列。 但是不能帶到第二或第三。
請看一下代碼。 我曾經填充兩個組合。 我不確定在哪里犯了錯誤。 請幫我。 預先感謝您。
<html><div><head><?php
$link = mysql_connect("localhost","db_user_name","db_user_passwd");
if (!$link) { die('Could not connect to Database: ' . mysql_error()); }
mysql_select_db("db_name",$link);
?>
<script type="text/javascript">
<?php
$category = array();
$query = "SELECT State_Name FROM Test_State";
$q_result = mysql_query($query);
if (!$q_result) { die('Could not make query: ' . mysql_error()); }
while($row = mysql_fetch_assoc($q_result)) { ?>
category[] = ["<?php $row; } ?>"];
function fillSelect(sel,ary,nxt){
if (ary&&sel.form){
var frm=sel.form,nme=sel.name.replace(/\d/g,""),i=Number(sel.name.replace(/\D/g,""))+1,nxt=frm[nxt],opts=sel.options,oary=[],z0=nxt==sel?0:1,z1=0,z1a;
while (frm[nme+i]){
frm[nme+i].length=1;
frm[nme+i].selectedIndex=0;
i++;
}
for (;z0<opts.length;z0++){
if (opts[z0].selected&&ary[opts[z0].value]){
oary=oary.concat(ary[opts[z0].value]);
} }
if (nxt){
for (;z1<oary.length;z1++) { nxt.options[z1+1]=new Option(oary[z1],oary[z1]); }
nxt.selectedIndex=0;
} } }
function getValue(isValue) { alert(isValue); }
function init() { fillSelect(document.forms[0]['List1'],category,'List1') }
navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);
</script></div></head>
<body>
<form action="">
<select name='List1' onchange="fillSelect(this,category,'List2')">
<option value="" selected>Select a country</option>
<?php
$category = "SELECT Country_Name FROM Test_Country";
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result))
{ ?>
<option value = "<?php echo $result['Country_Name']?>"><?php echo $result['Country_Name']?></option>
<?php } ?>
</select>
<select name='List2' onchange="getValue(this.value)">
<option selected>Select State</option>
</select>
</form></body></html>
我認為,最適合您的技術是使用示例代碼,只做一次更改:在PHP中構建頁面時,您想動態創建Categories數組。 您可以通過在PHP中創建數組,然后使用json_encode()將其轉換為javascript來非常巧妙地完成此操作。
在這里,我試圖向您展示如何構建可與現有Javascript一起使用的$categories
變量。 我尚未測試過,因此您應該會遇到一些錯誤。 但是技術很重要。
<html><div><head>
<?php
$link = mysql_connect("localhost","db_user_name","db_user_passwd");
if (!$link)
die('Could not connect to Database: ' . mysql_error());
mysql_select_db("db_name",$link);
$categories = array();
$countryQuery = "select State_Name FROM Test_State";
$countryResult = mysql_query($countryQuery);
while( $row = mysql_fetch_assoc($countryResult) )
$categories['startList'][] = $row['State_Name'];
/* I don't know your db schema, so I'm making it up as I go along
A db row of "California","SFO" becomes:
$categories["California"] = array("SFO");
Subsequent airports in California are appended to the same array.
*/
$airportQuery = "select Airport_Name, State_Name from Test_Airport";
$airportResult = mysql_query($airportQuery);
while( $row = mysql_fetch_assoc($airportResult) )
$categories[$row['State_Name']][] = $row['Airport_Name'];
// you can use the same technique to build a map of airports->resorts
?>
<!-- Now we just need to export the big PHP array as a Javascript variable.
Use the json_encode() function. -->
<script type="text/javascript">
$category = <?php echo json_encode($categories); ?> ;
</script>
我也為您提供一些其他說明:
首先,不要使用mysql庫。 不推薦使用新代碼。 改用mysqli或PDO 。
其次,我認為您對PHP和Javascript如何交互感到困惑。 總之,他們沒有。 PHP運行並創建頁面,然后瀏覽器加載頁面並運行Javascript。 您似乎希望Javascript與PHP交互,除非您采用AJAX技術,否則這不會發生。 您尚未准備好使用AJAX。
第三,如果您要讓其他人閱讀您的代碼,則應首先對其進行格式化。 凌亂的代碼很難閱讀,大多數人只會避免使用它,而不會回答您的問題。 如果您希望其他人幫助您編寫代碼,則需要願意花更多的精力解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.