简体   繁体   English

3使用Ajax MySQL的Downs Downs链

[英]3 Drop Downs chain using ajax mysql

I have 3 Pages Index.php findasset.php and findid.php. 我有3页Index.php findasset.php和findid.php。 I have 2 dropdowns and the last value will be echo out to another part of the page. 我有2个下拉菜单,最后一个值将回显到页面的另一部分。 I am using ajax to query the other dropdowns and it is partially working. 我正在使用ajax来查询其他下拉菜单,并且它部分起作用。

Most of it is dynamic and working besides device_category_name='$cId' on the findid page which should be replaced with $category but I wanted to show code as a working model. 除了在findid页面上的device_category_name ='$ cId'之外,大多数代码都是动态的并且可以正常工作,应该将其替换为$ category,但我想将代码显示为有效的模型。 I think the original start of my problem is on findasset page $category= isset($_GET['category']); 我认为问题的最初开始是在findasset页上$ category = isset($ _ GET ['category']);

When I try to echo out the variable on findid it echoes a "1" and not the word 当我尝试在findid上回显变量时,它回显“ 1”而不是单词

The index page has a dropdown pulled from mysql database that is working just fine. 索引页面上有一个从mysql数据库中拉出的下拉列表,它运行正常。 I have tagged the code as best as I could describe. 正如我所描述的,我已将代码标记为最佳。 Here is partially Working example . 这是部分工作示例 If you select Category-Drawing then either of the Assets it works, but it is because of on the findid page the query is partically hard coded and I dont want it to be hardcoded. 如果选择Category-Drawing,则可以使用任何一种资产,但这是因为在findid页面上查询是部分硬编码的,因此我不希望对其进行硬编码。

I know, I am so close to getting this figured out but I am stuck. 我知道,我很想弄明白这一点,但我被困住了。 Could you help me out? 你能帮我吗?

Index.php 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 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 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 ??

} ?>

I think your problem is, you don't understand what "isset()" is doing: 我认为您的问题是,您不了解“ isset()”在做什么:

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

http://php.net/isset determines weither a variable or an index exists (and is not NULL), the return value of isset is boolean, this mean either true or false. http://php.net/isset确定是否存在变量或索引(且不为NULL),isset的返回值为boolean,表示true或false。 In your case it seems to be true, because your echo shows an 1. 在您的情况下,这似乎是正确的,因为您的回声显示为1。

I think you try to do this: 我认为您尝试这样做:

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

On the other hand, you have heavy security issues in your code 另一方面,您的代码中存在严重的安全性问题

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

You can't just use $assets unfiltered. 您不能只使用未过滤的$ assets。 Please google for SQL Injection for more informations. 请谷歌的SQL注入更多的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM