我目前正在修改一个开箱即用的php / jQuery自动提供脚本。我有以下问题:
我有两个带autosuggest的表单字段。第一个字段是供用户输入城市。所以我的php文件autosuggest.php
查询数据库并查找具有parent = 9
的匹配项(这是所有城市)。现在对于用户输入school
的第二个字段,我希望我的php通过DB查询并找到将输入的城市作为父级的学校。
我试图通过将从PHP返回的String存储到javascript(当用户已进入城市时)到SESSION
变量来解决此问题。然后在autosuggest.php
查询时使用此会话变量作为父级,以查找SECOND表单的匹配项。
但我不能让这个工作。我收到这个错误:
Catchable fatal error: Object of class mysqli_result could not be converted to string in
autosuggest.php on line 47 (LIKE '$queryString%').
但如果我在$parent = $termquery;
中注释掉autosuggest.php
行,就会消失。
这是我的代码:
index.php包含此sript:
<?php session_start();?>
<script>
var globEl = "";
function suggest(inputString){
var activeEle = document.activeElement.id;
globEl = activeEle;
if(inputString.length == 0) {
$('#suggestions').fadeOut();
} else if(activeEle == "city") {
$('#city').addClass('load');
$.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').fadeIn();
$('#suggestionsList').html(data);
$('#city').removeClass('load');
}
});
} else if(activeEle == "school") {
$('#school').addClass('load');
$.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').fadeIn();
$('#suggestionsList').html(data);
$('#school').removeClass('load');
}
});
}
}
function fill(thisValue) {
if (globEl == 'city') {
$('#city').val(thisValue);
$.post('update_session.php', {parentnamee: thisValue});
setTimeout("$('#suggestions').fadeOut();", 0);
} else if(globEl == 'school') {
$('#school').val(thisValue);
$.post('update_session.php', {parentnamee: thisValue});
setTimeout("$('#suggestions').fadeOut();", 0);
}
}
</script>
update_session.php :
<?php
session_start();
$_SESSION['foreldrenavnn'] = $_POST['foreldrenavnn'];
?>
autosuggest.php :
<?php
session_start();
$db_host = 'host';
$db_username = 'user';
$db_password = 'passw';
$db_name = 'db';
$parent = 9;
$db = new mysqli($db_host, $db_username ,$db_password, $db_name);
if(!$db) {
echo 'Could not connect to the database.';
} else {
if(isset($_SESSION['parentnamee'])) {
$parentstring = $db->real_escape_string($_SESSION['parentnamee']);
if(strlen($parentstring) >0) {
$termquery = $db->query("
SELECT wp_terms.term_id
FROM wp_terms
WHERE wp_terms.name='$parentstring%'
");
$parent = $termquery;
}
} else {
// do nothing
}
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = $db->query("
SELECT wp_terms.name
FROM wp_terms
INNER JOIN wp_term_taxonomy
ON wp_terms.term_id = wp_term_taxonomy.term_id
AND wp_term_taxonomy.parent=$parent
WHERE wp_terms.name
LIKE '$queryString%'
LIMIT 10
");
if(mysqli_num_rows($query) > 0) {
if($query) {
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.addslashes($result->name).'\');">'.$result->name.'</li>';
}
echo '</ul>';
} else {
echo 'OOPS we had a problem :(';
}
} else {
echo '<ul>';
echo '<span style="color:#0196E3";>'.$queryString.'</span> no matches.';
echo '</ul>';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
任何人都有任何想法我做错了什么?真的很感谢你的帮助!