我有3页Index.php findasset.php和findid.php。我有2下拉菜单,最后的值将被回显到页面的另一部分。我使用ajax来查询其他下拉框,它部分工作。
大部分是动态的,除了device_category_name='$cId'在findid页面上应该用$category替换,但我想显示代码作为一个工作模型。我认为我的问题的最初开始是在findasset页面$category= isset($_GET['category']);
当我试图在findid上回显变量时,它回显的是"1"而不是单词
索引页有一个下拉从mysql数据库,工作得很好。我已经尽我所能地标记了代码。这里是部分工作示例。如果你选择类别绘制,那么任何一个资产都可以工作,但这是因为在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> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </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的返回值是布尔值,这意味着true或false。在你的例子中,它似乎是真的,因为你的echo显示了一个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 Injection获取更多信息。