weixin_33711647 2015-08-05 12:45 采纳率: 0%
浏览 24

使用PHP和mysql进行AJAX查询

$(document).ready(function(){
    $('input.phonebook_user').phonebook_user({
        name: 'phonebook_user',
        remote:'search.php?type=PHONEBOOK&key=%QUERY%',
        limit : 10
    });
});

and

<form method="post">
<table border="0">
<tr>
<td><input type="text" name="phonebook_name" placeholder="PhoneBook Name" required /></td>
</tr>
<tr>
<td><input type="text" name="phonebook_user" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Type username to manage phonebook"></td>
</tr>
<tr>
<td><button type="submit" name="com_btn-phbook-create">Create PhoneBook</button></td>
</tr>
</table>
</form>

and

$TYPE=$_GET['TYPE']; // user or company
if($TYPE=="USER") {
    $KEY=$_GET['key'];
    $array = array();
    $query=mysql_query("SELECT * FROM `users` WHERE `email` LIKE '%{$key}%'");
    while($row=mysql_fetch_assoc($query))
        {
            $array[] = $row['email'];
        }
    echo json_encode($array);
}
else if($TYPE=="COMPANY") {
    $KEY=$_GET['key'];
    $array = array();
    $query=mysql_query("SELECT * FROM `company` WHERE `company` LIKE '%{$key}%'");
    while($row=mysql_fetch_assoc($query))
        {
            $array[] = $row['name'];
        }
    echo json_encode($array);
}
else if($TYPE=="PHONEBOOK") {
    $KEY=$_GET['key'];
    $array = array();
    $query=mysql_query("SELECT * FROM `users` WHERE `username` LIKE '%{$key}%'");
    while($row=mysql_fetch_assoc($query))
        {
            $array[] = $row['user_id'];
        }
    echo json_encode($array);
}

I'm trying to pull the e-mail of a user name that is getting typed and the box does not seem to show the drop down list of user names to select when you start to type in a user name. Just curious where I am going wrong with this code.

My goal is so that a user types in a user name, the drop down list shows possible matches and when you select that name, it pulls their e-mail which is then submitted to another query.

I had it working when I was just using ?key=%QUERY% in the javascript.

Am I doing my if and else statements wrong?

  • 写回答

1条回答 默认 最新

  • weixin_33708432 2015-08-05 12:57
    关注

    Thought that problem is due to %. sending the search string enclosed in %

    remote:'search.php?type=PHONEBOOK&key=%QUERY%',
    

    And also enclosed it in % in query

    SELECT * FROM `users` WHERE `email` LIKE '%{$key}%'
    

    Also check the cases of variables in url 'type' and in php code $_GET['TYPE'] also $KEY and $key

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛