Jump to content

Aucomplete with id and name


gmc1103

Recommended Posts

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.