dongzhao1930 2013-06-14 21:39
浏览 25

3使用ajax mysql删除Downs链

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:
" + 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:
" + 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 ??

} ?>
  • 写回答

1条回答 默认 最新

  • duanchifo2866 2013-06-14 21:58
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程