gmc1103 Posted January 13, 2016 Share Posted January 13, 2016 Hi I'm trying to use the followwing code to get the user name but i need also the user_id. The code is the following <script type="text/javascript"> function autocomplet() { var min_length = 2; var keyword = $('#nome_id').val(); if (keyword.length >= min_length) { $.ajax({ url: 'getFromDB/getFromDatabase.php', type: 'POST', data: {keyword:keyword}, success:function(data){ $('#nome_list_id').show(); $('#nome_list_id').html(data); } }); } else { $('#nome_list_id').hide(); } } function set_item(item) { $('#nome_id').val(item); $('#nome_list_id').hide(); } </script> and in my pdo i have this $keyword = '%'.$_POST['keyword'].'%'; if ($keyword!= null) { $sql = "SELECT * FROM utilizador WHERE nome LIKE (:keyword) and estado = 1 ORDER BY idutilizador ASC LIMIT 0, 10"; $query = $DB_con->prepare($sql); $query->bindParam(':keyword', $keyword, PDO::PARAM_STR); $query->execute(); $list = $query->fetchAll(); foreach ($list as $rs) { $nome = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['nome']); $iduser = $rs['idutilizador']; echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['nome']).'\')">'.$nome.'</li>'; } } In the following line i need to pass the variable $iduser echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['nome']).'\')">'.$nome.'</li>'; any help? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2016 Share Posted January 14, 2016 you can add it as a data- attribute to the li item: foreach ($list as $rs) { $nome = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['nome']); $iduser = $rs['idutilizador']; $stringOut = <<<STROUT <li data-id="{$iduser}" onclick="set_item('{$rs['nome']})'>{$nome}</li> STROUT; echo $stringOut; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.