Sven
Sven

Reputation: 63

JQUERY autocomplete get id instead of value

I have a mysql database where I would like to use the autocomplete one. So far I am able to receive the results of the query. For example this is the result: [{"LNAME":"Napoleon","id":"1"}] But I want the id of the customer in this case to be filled in another input field of my form. (so in Napoleons case I want the "1" to be filled in to my clienteID field of the form) At the moment the field does not get updated.

Here's the HTML part:

    <script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>

<script>
$(document).ready(function(){
 $("#tag").autocomplete("autocomplete.php", {
        minLength: 3,
        select: function(event, ui) {
            $('#clienteId').val(ui.item.id);            
                 },
    });
});
</script>

<form name='form_update_details' id='form_update_details' method='post' action=''>
<input type='text' name='clienteName' id='tag'>
<input type='text' name='clienteId' id='clienteId'>
</form>

Then the php part:

<?php

include("include/db.php");

    $q=$_GET['q'];
    $my_data=mysql_real_escape_string($q);

    $sql="SELECT ID, CONTACTS_LNAME, CONTACTS_FNAME FROM CRM_CONTACTS WHERE CONTACTS_LNAME LIKE '%$my_data%' or CONTACTS_FNAME LIKE '%$my_data%' ORDER BY CONTACTS_LNAME";
    $result = mysqli_query($coni,$sql) or die(mysqli_error());

    if($result)
    {
        while($row=mysqli_fetch_array($result))
        {
            $data[] = array(
                'LNAME' => $row['CONTACTS_LNAME'],
                'id' => $row['ID']
        );
        }
        echo json_encode($data);
        flush();
    }
?>

Upvotes: 0

Views: 3132

Answers (2)

Suchit kumar
Suchit kumar

Reputation: 11869

Try with a sample data:use label and value as keys instead of LNAME and id. Use event.preventDefault(); to set the label in #tag field otherwise by default it will set the value.

var data = [
        {
            value: "1",
            label: "jQuery"
        },
        {
            value: "2",
            label: "jQuery UI"
        },
        {
            value: "3",
            label: "Sizzle JS"
        }
    ];

$(document).ready(function(){
 $("#tag").autocomplete( {
        source: data,
        minLength: 3,
        select: function(event, ui) {
            event.preventDefault();
            $("#tag").val(ui.item.label);
            $('#clienteId').val(ui.item.value);            
            }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<form name='form_update_details' id='form_update_details' method='post' action=''>
<input type='text' name='clienteName' id='tag'>
<input type='text' name='clienteId' id='clienteId'>
</form>

Upvotes: 2

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26288

Try this:

$( function() {
    var projects = [
        {
            value: "1",
            label: "jQuery"
        },
        {
            value: "2",
            label: "jQuery UI"
        },
        {
            value: "3",
            label: "Sizzle JS"
        }
    ];

    $( "#project" ).autocomplete({
        minLength: 0,
        source: projects,
        focus: function( event, ui ) {
            $( "#project" ).val( ui.item.label );
            return false;
        },
        select: function( event, ui ) {
            $( "#project" ).val( ui.item.label );
            $( "#project-id" ).val( ui.item.value );

            return false;
        }
    })
    .autocomplete( "instance" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<div>" + item.label + "</div>" )
        .appendTo( ul );
    };
  });

Html:

<div id="project-label">Select a project (type "j" for a start):</div>
<input id="project">
<input type="text" id="project-id">

Demo Fiddle

Upvotes: 0

Related Questions