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 7778981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:32:05+00:00 2026-06-01T18:32:05+00:00

I have done autocomplete for my php code usign ajax.I used this code for

  • 0

I have done autocomplete for my php code usign ajax.I used this code for autocompleting one fileld.Can I use the same code for autocompleting more than one field…

Code contains ajax code is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Autocomplete.....</title>

<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>

<script type="text/javascript">
    function lookup(inputString) 
    {
        if(inputString.length == 0) 
        {
            // Hide the suggestion box.
            $('#suggestions').hide();
        }
        else 
        {
            $.post("rpc.php", {queryString: ""+inputString+""}, function(data)
            {
                if(data.length >0) 
                {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').html(data);
                }
            });
        }
    } // lookup

    function fill(thisValue) 
    {
        $('#inputString').val(thisValue);
        setTimeout("$('#suggestions').hide();", 200);
    }

</script>

<style type="text/css">

    body 
    {
        font-family: Helvetica;
        font-size: 11px;
        color: #000;
    }

    h3 
    {
        margin: 0px;
        padding: 0px;   
    }

    .suggestionsBox 
    {
        position: relative;
        left: 30px;
        margin: 10px 0px 0px 0px;
        width: 200px;
        background-color: #212427;
        -moz-border-radius: 7px;
        -webkit-border-radius: 7px;
        border: 2px solid #000; 
        color: #fff;
    }

    .suggestionList 
    {
        margin: 0px;
        padding: 0px;
    }

    .suggestionList li 
    {

        margin: 0px 0px 3px 0px;
        padding: 3px;
        cursor: pointer;
    }

    .suggestionList li:hover 
    {
        background-color: #659CD8;
    }

</style>



</head>

<body>


<div>
        <form>
            <div>
                Type your county:
                <br />
                <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
            </div>

            <div class="suggestionsBox" id="suggestions" style="display: none;">
                <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
                <div class="suggestionList" id="autoSuggestionsList">
                    &nbsp;
                </div>
            </div>
        </form>
    </div>
</body>
</html>

Code for database connection page is

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("name",$con);

if(!$con) 
{
        echo 'ERROR: Could not connect to the database.';
}
else 
{
        if(isset($_POST['queryString'])) 
        {

        $queryString = mysql_real_escape_string($_POST['queryString']);

        if(strlen($queryString) >0) 
        {

        $query = mysql_query("SELECT cname FROM country WHERE cname LIKE '$queryString%' LIMIT 10");
                if($query) 
                {
                    while($result= mysql_fetch_object($query))
                    {

        echo '<li onClick="fill(\''.$result->cname.'\');">'.$result->cname.'</li>';
                    }
                } 
                else 
                {
                    echo 'ERROR: There was a problem with the query.';
                }
        } 
        else 
        {
                // Dont do anything.
        }
        } 
        else 
        {
            echo 'There should be no direct access to this script!';
        }
}

?>
  • 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-06-01T18:32:07+00:00Added an answer on June 1, 2026 at 6:32 pm

    Yes, you can use the code to autocomplete more than one field.

    The quick and dirty way:

    HTML:

    <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill(this);" />
    <input type="text" size="30" value="" id="inputString2" onkeyup="lookup(this.value);" onblur="fill(this);" />
    <input type="text" size="30" value="" id="inputString3" onkeyup="lookup(this.value);" onblur="fill(this);" />
    

    Javascript:

    function fill(element,thisValue) 
    {
        element.value = thisValue;
        setTimeout("$('#suggestions').hide();", 200);
    }
    

    The jQuery and more efficient way (based on your jQuery version 1.2.1:

    HTML:

    <input type="text" size="30" value="" class="autoupdate" name="field1" />
    <input type="text" size="30" value="" class="autoupdate" name="field2" />
    <input type="text" size="30" value="" class="autoupdate" name="field3" />
    

    Javascript (using jQuery 1.2.1):

    // fill on blur!
    $(".autoupdate").blur(function()
    { 
        $(this).val(thisValue);
        setTimeout("$('#suggestions').hide();", 200);
    });
    
    // lookup value onkeyup!
    $(".autoupdate").keypress(function()
    {
        var fieldvalue = $(this).val();
        if ( fieldvalue.length > 0 )
        {
            // hide selections box
            $('#suggestions').hide();
    
        } else {
    
            $.post("rpc.php", {queryString: ""+fieldvalue+""}, function(data)
            {
                if(data.length >0) 
                {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').html(data);
                }
            });
    
        }
    });
    

    Further optimizations:

    AJAXing a URL as you keypress is unnecessary. If you don’t mind an extra second to auto-fill; it would be better to submit PHP forms onblur instead. Additionally, you can also take advantage of JSON and essentially get all the data you need in one single AJAX call which as a end result makes the form’s footprint have 1 AJAX call and 1 MySQL database query. Otherwise, your simply pounding the server on each keyup event that is fired.

    — OR —

    Alternatively you can setup a delay that waits 2-3 seconds before submitting the PHP form. It would then also need to ensure that the setTimeout is cleared upon additional keypresses so as to prevent unnecessary pileup of delayed AJAX calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an autocomplete text box that users can type an item code into
I have done a bit of testing on this myself (During the server side
I have done a bit of research into this and it seems that the
I have done this: $ z() { echo 'hello world'; } How do I
I have done jQuery and Ajax, but I am not able to get the
I have done the following code in JavaScript to put focus on the particular
I have a page with an ajax filled autocomplete field, if an extra addition
I have a form here that I can get to autocomplete with hard-coded items
I have done a heap of searching but can't seem to get anywhere. I
This query will be done in a cached autocomplete text box, possibly by thousands

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.