Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313046
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:04:10+00:00 2026-05-17T22:04:10+00:00

I need an example of how to code a jQuery autocomplete to populate product_id

  • 0

I need an example of how to code a jQuery autocomplete to populate product_id while showing the product_name calling an ajax page “remote.php”

<input name="product_name" id="product_name" type="text" value="" />
<input name="product_id" id="product_id" type="hidden" value="" />


remote.php:

$partial = addslashes($_POST['partial_search']);
$myDataRows = array(); 
$result = mysql_query ("SELECT product_id, product_name FROM products 
   WHERE product_name like "%$partial%");
while ($row = mysql_fetch_row($result)) {
   array_push($myDataRows, $row);
}
$ret = json_encode ($myDataRows);
echo $ret;

I’m not sure how to code the jQuery autocomplete and if I need to change remote.php

thanks

ADDED LATER:

I worked out another solution:

<script type="text/javascript">
function nqi_search (type, id_name, text_name)
{
    $( "#"+text_name ).autocomplete({
        source: "remote.php?&t="+type,
        minLength: 1,
        select: function( event, ui ) {
            $( "#"+id_name ).val(ui.item.id );
        }
    });
}
</script>

<script type="text/javascript">
jQuery(document).ready(function() {

    nqi_search ("product_search", "product_id", "product_name");

    // also you can have many on one page with:
    nqi_search ("vendor_search", "vendor_id", "vendor_name");   


});
</script>

There’s one problem. it doesn’t seem to work if the nqi_search function is put into a .js file. I have no idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:04:10+00:00Added an answer on May 17, 2026 at 10:04 pm

    This is how I do it:

    Note, I’ve coded a special feature where the json can flag an item as a message instead and in this way you can put messages in the list (eg I put a “Addition X items not shown” for long lists). To use the message feature, but the text in the label field and a true boolean for the message field.

    To use this on the page I just have

    setupAutocomplete(<id of textbox>,<path to service>);
    

      $.ajaxSetup({
          type: "POST",
          contentType: "application/json; charset=utf-8",
          data: "{}",
          dataFilter: function(data) {
             var msg;
    
             if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
                msg = JSON.parse(data);
             else
                msg = eval('(' + data + ')');
    
             if (msg.hasOwnProperty('d'))
                return msg.d;
             else
                return msg;
          },
          error: function(msg) {
             $('#error').html(msg.responseText)
          }
       });
    
    
    // remove this to get rid of custom message handling
    $.widget("custom.redcomplete", $.ui.autocomplete, {
       _renderMenu: function(ul, items) {
          var self = this;
          $.each(items, function(index, item) {
    
             if (item.message)
                ul.append("<li class='ui-menu-item special-red'>&nbsp;" + item.label + "</li>");
             else
                self._renderItem(ul, item)
          });
       }
    
    
    function setupAutocomplete(inID, inURL) {
       var myTB = $("[id$='_" + inID + "']");
       // change redcomplete to autocomplete to get rid of message handling
       myTB.redcomplete({
          source: function(request, response) {
             $.ajax({
                url: inURL,
                data: "{'filter': '" + request.term + "'}",
                success: function(data) {
                   response($.map(data, function(item) {
                      return {
                         label: item.text,
                         value: item.id,
                         // remove this line and the , above to get rid of message handling
                         message: item.message
                      };
                   }));
                }
             })
          },
          delay: 500,
          minLength: 3,
          focus: function(event, ui) {
             myTB.val(ui.item.label);
             return false;
          },
          select: function(event, ui) {
            // action for the select here.
             return false;
          },
          open: function() {
             $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
          },
          close: function() {
             $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
          }
    
       });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm posting simple version of php and Jquery code of the page that I
I need a code example or library which would parse Accept-Language header and return
I need to plot multiple sphere and I was using the example code from
So, I need to write an example code using the decorator dessign pattern and
I need to run some code, for example, to detect browser prefered languages and
If I create a JQuery widget (code example below), and then define a public
I was reading the jQuery UI API for datepicker. The example code has something
I'm attempting to use JQuery autocomplete and need to get the id of the
I need code example for: custom JavaScript function that takes a single object as
I need a simple jQuery code that will add an empty label tag next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.