繁体   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