#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    379
    Rep Power
    47

    Type-ahead form field


    Hi:

    I am trying to use a "type-ahead" form field that I downloaded which works but I would like the form to use a different field than what is displayed in the text box and I'm not sure how to do that.

    The original Autocompleter can be found here http://www.nodstrum.com/2007/09/19/a...mment-page-16/

    As it is written, it looks up the typed string in a countries sql table and returns the list of country names in the suggestion list. When a company name is selected, it fills the form field with the company name... works great!

    The problem is that, in my application, there may be multiple records with the same "name". So, while I want the list to display the names and the form to fill in the names, I really need to submit the "id" to the function and I am not sure how to do that.

    So, the form field on my page looks like:

    PHP Code:
    Type any part of a company name:
    <
    input type="text" name "company" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
                
    <
    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
    And I have the Autocomplete configured as such:
    PHP Code:
    $query $db->query("SELECT company_id, company_name  
                    FROM crm_companies 
                    WHERE company_name LIKE '%
    $queryString%' LIMIT 20");
    if(
    $query) {
    while (
    $result $query ->fetch_object()) {
    echo 
    '<li onClick="fill(\''.$result->company_name.'\');">'.$result->company_name.'</li>';

    This works perfectly. As I type a company name, the suggestion box filters on my string. When I click a name, that name fills the form field. Similarly, if I change the "fill" line to:
    PHP Code:
    echo '<li onClick="fill(\''.$result->company_id.'\');">'.$result->company_name.'</li>'
    The suggestion list shows the company names and when I click on a name, the form field is populated with the company ID.

    So, it's close but what I need is for the suggestion list to show company names, when a name is clicked the name should populate the form field but the ID should be used as when the form is submitted.

    Is that possible? Any thoughts?

    Thanks tons!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    this doesn't make a lot of sense. If there may be multiple IDs for the same name, then what is "the ID" that should be filled in?

    If you choose an ID by whatever criterion, why don't you simply do that in after the name has been submitted?

    Apart from that, I see plenty of security holes.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    379
    Rep Power
    47
    Well, my thinking was this...

    If this is a company contacts database there might be multiple "Mike's Pizza" or such. So if the form displays company name and city but the unique company ID is captured for subsequent SQL search, it would avoid confusion.

    Does that make sense? Is there a better way to do it?

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo