【问题标题】:3 Drop Downs chain using ajax mysql3 使用 ajax mysql 的下拉链
【发布时间】:2013-06-11 15:27:44
【问题描述】:

我有 3 页 Index.php findasset.php 和 findid.php。我有 2 个下拉菜单,最后一个值将回显到页面的另一部分。我正在使用 ajax 来查询其他下拉列表,它正在部分工作。

除了 findid 页面上的 device_category_name='$cId' 之外,其中大部分是动态的并且可以工作,应该用 $category 替换,但我想将代码显示为工作模型。我认为我的问题最初是在 findasset 页面 $category=isset($_GET['category']);

当我尝试在 findid 上回显变量时,它回显的是“1”而不是单词

索引页面有一个从 mysql 数据库中提取的下拉菜单,它工作得很好。我已经尽可能地标记了代码。 这是部分Working example。如果您选择 Category-Drawing,那么其中任何一个资产都可以工作,但这是因为在 findid 页面上,查询是部分硬编码的,我不希望它被硬编码。

我知道,我非常接近解决这个问题,但我被卡住了。你能帮帮我吗?

索引.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 ??

} ?>

【问题讨论】:

    标签: php mysql ajax dynamic html-select


    【解决方案1】:

    我认为你的问题是,你不明白“isset()”在做什么:

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

    http://php.net/isset 判断变量或索引是否存在(并且不为 NULL),isset 的返回值是布尔值,这意味着真或假。在您的情况下,这似乎是正确的,因为您的回声显示为 1。

    我认为你尝试这样做:

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

    另一方面,您的代码中存在严重的安全问题

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

    您不能只使用未过滤的 $assets。请 google SQL 注入 了解更多信息。

    【讨论】:

    • 感谢您对安全问题的建议。我将不得不对此进行调查。我似乎无法让代码使用这一行 $category=isset($_GET['category']) ? $_GET['category'] : null;
    猜你喜欢
    • 2010-12-30
    • 2017-08-11
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2012-01-12
    • 2018-02-06
    • 2018-12-15
    相关资源
    最近更新 更多