简体   繁体   中英

AJAX query with php and mysql

$(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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM