无法过滤组合框中的字符

时间:2014-02-14 11:14:41

标签: javascript php html mysql jcombobox

有人可以帮我吗?我的sql代码只有在我只匹配2 = 2这样的整数时才有效,但如果我把它更改为像橙色=橙色它就不会工作......任何人都可以帮我弄清楚我的代码是不是错了。

的index.php:

<script type="text/javascript" src="jquery/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="jquery.jCombo.min.js"></script>
<form>
Caraga Region: <select name="region" id="region"></select>
Municipalities: <select name="town" id="town"></select>
Unique ID: <select name="uniq_id" id="uniq_id"></select> <br />
</form>
<script type="text/javascript">
$( document ).ready(function() { 
   $("#region").jCombo({ url: "getRegion.php" } );
   $("#town").jCombo({ url: "getTown.php?townid=", parent: "#region", selected_value : '510' } );
   $("#uniq_id").jCombo({ url: "getID.php?unqid=", parent: "#town", data: String, selected_value : '150' } );
});
</script>

getRegion.php:

<?php

    // Connect Database
    mysql_connect("localhost","root","");     
    mysql_select_db("klayton");

    // Execute Query in the right order  
    //(value,text)
    $query = "SELECT id, municipalities FROM regions";
    $result = mysql_query($query);
    $items = array();
    if($result && mysql_num_rows($result)>0) {
        while($row = mysql_fetch_array($result)) {
            $option = array("id" => $row[0], "value" => htmlentities($row[1]));
            $items[] = $option; 
        }        
    }
    mysql_close();
    $data = json_encode($items); 
    // convert into JSON format and print
    $response = isset($_GET['callback'])?$_GET['callback']."(".$data.")":$data; 
    echo $data;
?>

getTown.php:

<?php 

// Connect Database 
mysql_connect("localhost","root",""); 
mysql_select_db("klayton"); 

// Get parameters from Array 

$townid = !empty($_GET['townid']) 
?intval($_GET['townid']):0;
// if there is no city selected by GET, fetch all rows 
$query = "SELECT town FROM towns WHERE tcode = $townid"; 

// fetch the results 
$result = mysql_query($query); 
$items = array(); 
if($result && mysql_num_rows($result)>0) { 
while($row = mysql_fetch_array($result)) { 
$option = array("id" => $row['town'], "value" => htmlentities($row['town'])); 
$items[] = $option; 
} 
}
    mysql_close();
    $data = json_encode($items); 
    echo $data;
?>

getID.php:问题出在此代码中。如果我将字符与字符匹配,它将无效,只有在integer=integer

时它才有效
<?php 

// Connect Database 
mysql_connect("localhost","root",""); 
mysql_select_db("klayton"); 

// Get parameters from Array 

$unqid = !empty($_GET['unqid']) 
?intval($_GET['unqid']):0;
// if there is no city selected by GET, fetch all rows 
$query = "SELECT uid, unq_pos_id FROM tb_uniqid WHERE tb_uniqid.uid = '$unqid'"; 

// fetch the results 
$result = mysql_query($query); 
$items = array(); 
if($result && mysql_num_rows($result)>0) { 
while($row = mysql_fetch_array($result)) { 
$option = array("id" => $row['uid'], "value" => htmlentities($row['unq_pos_id'])); 
$items[] = $option; 
} 
}
    mysql_close();
    $data = json_encode($items); 
    echo $data;
?>

(uid)字段存储有字符值,就像(town)字段一样。我想匹配它,但它不起作用。

1 个答案:

答案 0 :(得分:1)

尝试在 getID.php 替换:

$unqid = !empty($_GET['unqid']) 
?intval($_GET['unqid']):0;

使用:

$unqid = !empty($_GET['unqid']) 
?$_GET['unqid']:0;

如果您希望能够匹配字符串以及整数。您看,intval()仅返回变量的整数值,因此当您将字符串发送到该页面时,您将删除其他字符,因此您无法将任何内容与之前的代码进行匹配。