简体   繁体   中英

3 Drop Downs chain using ajax mysql

I have 3 Pages Index.php findasset.php and findid.php. I have 2 dropdowns and the last value will be echo out to another part of the page. I am using ajax to query the other dropdowns and it is partially working.

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. I think the original start of my problem is on findasset page $category= isset($_GET['category']);

When I try to echo out the variable on findid it echoes a "1" and not the word

The index page has a dropdown pulled from mysql database that is working just fine. 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.

I know, I am so close to getting this figured out but I am stuck. Could you help me out?

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 ??

} ?>

I think your problem is, you don't understand what "isset()" is doing:

$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. In your case it seems to be true, because your echo shows an 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. Please google for SQL Injection for more informations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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