# HG changeset patch # User Dan # Date 1193232859 14400 # Node ID 996572e55dc94fecf0b46fd196bbb97b12d0f277 # Parent 9dbbec5e6096c6a3b18db93cf8be1682db5d2f48 Update ajax.php to use the new username completion algo diff -r 9dbbec5e6096 -r 996572e55dc9 ajax.php --- a/ajax.php Sun Oct 14 20:51:15 2007 -0400 +++ b/ajax.php Wed Oct 24 09:34:19 2007 -0400 @@ -33,35 +33,50 @@ define('ENANO_ROOT', dirname($filename)); require(ENANO_ROOT.'/includes/functions.php'); require(ENANO_ROOT.'/includes/dbal.php'); + require(ENANO_ROOT.'/includes/json.php'); $db = new mysql(); $db->connect(); - // should be connected now + // result is sent using JSON + $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); + $return = Array( + 'mode' => 'success', + 'users_real' => Array() + ); + + // should be connected to the DB now $name = (isset($_GET['name'])) ? $db->escape($_GET['name']) : false; if ( !$name ) { - die('userlist = new Array(); errorstring=\'Invalid URI\''); + $return = array( + 'mode' => 'error', + 'error' => 'Invalid URI' + ); + die( $json->encode($return) ); } - $q = $db->sql_query('SELECT username,user_id FROM '.table_prefix.'users WHERE lcase(username) LIKE lcase(\'%'.$name.'%\');'); + $allowanon = ( isset($_GET['allowanon']) && $_GET['allowanon'] == '1' ) ? '' : ' AND user_id > 1'; + $q = $db->sql_query('SELECT username FROM '.table_prefix.'users WHERE lcase(username) LIKE lcase(\'%'.$name.'%\')' . $allowanon . ' ORDER BY username ASC;'); if ( !$q ) { - die('userlist = new Array(); errorstring=\'MySQL error selecting username data: '.addslashes(mysql_error()).'\''); + $return = array( + 'mode' => 'error', + 'error' => 'MySQL error selecting username data: '.addslashes(mysql_error()) + ); + die( $json->encode($return) ); } - if($db->numrows() < 1) - { - die('userlist = new Array(); errorstring=\'No usernames found\';'); - } - echo 'var errorstring = false; userlist = new Array();'; $i = 0; while($r = $db->fetchrow()) { - echo "userlist[$i] = '".addslashes($r['username'])."'; "; + $return['users_real'][] = $r['username']; $i++; } $db->free_result(); // all done! :-) $db->close(); + + echo $json->encode( $return ); + exit; }