簡體   English   中英

3使用Ajax MySQL的Downs Downs鏈

[英]3 Drop Downs chain using ajax mysql

我有3頁Index.php findasset.php和findid.php。 我有2個下拉菜單,最后一個值將回顯到頁面的另一部分。 我正在使用ajax來查詢其他下拉菜單,並且它部分起作用。

除了在findid頁面上的device_category_name ='$ cId'之外,大多數代碼都是動態的並且可以正常工作,應該將其替換為$ category,但我想將代碼顯示為有效的模型。 我認為問題的最初開始是在findasset頁上$ category = isset($ _ GET ['category']);

當我嘗試在findid上回顯變量時,它回顯“ 1”而不是單詞

索引頁面上有一個從mysql數據庫中拉出的下拉列表,它運行正常。 正如我所描述的,我已將代碼標記為最佳。 這是部分工作示例 如果選擇Category-Drawing,則可以使用任何一種資產,但這是因為在findid頁面上查詢是部分硬編碼的,因此我不希望對其進行硬編碼。

我知道,我很想弄明白這一點,但我被困住了。 你能幫我嗎?

Index.php

function getXMLHTTP() { //function to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}

function getcategory(category) {        

    var strURL="findasset.php?category="+category;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('assetdiv').innerHTML=req.responseText;                     
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }       
}
function getid(category,asset) {        
    var strURL="findid.php?category="+category+"&asset="+asset;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('iddiv').innerHTML=req.responseText;                        
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Category</td>
<td  width="150"><select name="category" onChange="getcategory(this.value)">
<?
require "config.php";// connection to database 

$query = "SELECT DISTINCT device_category_name FROM fgen_structures ORDER BY device_category_name ASC";
$result = mysql_query($query);

while ($myrow = mysql_fetch_array($result))
{




echo "<option value='$myrow[device_category_name]'>$myrow[device_category_name]</option>";
}

?>
    </select></td>
  </tr>
<tr style="">
  <td>Asset</td>
  <td ><div id="assetdiv"><select name="asset" >
<option>Select Category First</option>
    </select></div></td>
 </tr>
<tr style="">
 <td>ID</td>
 <td ><div id="iddiv"></div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

findasset.php

$category= isset($_GET['category']);// Could be the Start of the PROBLEM
$cate=$_GET['category'];
require "config.php";// connection to database 


$query="SELECT * FROM fgen_structures WHERE device_category_name='$cate'";
$result=mysql_query($query);

?>
<select name="asset" onchange="getid(<?=$category;?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<? echo $row['device_type_name'];?>><? echo $row['device_type_name'];?></option>
<? } ?>
</select>

findid.php

<? 
$category=isset($_GET['category']); // This is where I think the problem is as well!!!!
$asset=isset($_GET['asset']);


$cate=$_GET['category'];

$assets=$_GET['asset'];

$cId='Drawing'; //If Hard Coded works

require "config.php";// connection to database 

$query="SELECT * FROM fgen_structures WHERE device_category_name='$cId' AND device_type_name='$assets'"; // Currently hardcoded with $cid and it works but I need it dynamic     with $cate or $category
$result=mysql_query($query);




while($row=mysql_fetch_array($result)) { 
echo $row['fgen_structure_id'];
 //echo $category; // This displays a 1 ??

} ?>

我認為您的問題是,您不了解“ isset()”在做什么:

$category=isset($_GET['category']);

http://php.net/isset確定是否存在變量或索引(且不為NULL),isset的返回值為boolean,表示true或false。 在您的情況下,這似乎是正確的,因為您的回聲顯示為1。

我認為您嘗試這樣做:

$category=isset($_GET['category']) ? $_GET['category'] : null;

另一方面,您的代碼中存在嚴重的安全性問題

$query="SELECT * FROM fgen_structures WHERE device_category_name='$cId' AND device_type_name='$assets'";

您不能只使用未過濾的$ assets。 請谷歌的SQL注入更多的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM