简体   繁体   中英

jQuery Autofill textbox with information from another autofill

I am having an issue with jQuery autocomplete. Basically I have a search bar, and when you type in what you're looking for the jQuery code I have calls a php script which does a MySQL query and returns everything I need and fills in the text boxes accordingly. What I then want to do is take the value I receive from that autocomplete, and use it in another autocomplete to fill in more data. The tricky part is that the data I need to get with the 2nd query is located in a different table than the first query, which share a relationship. My question is do I need a completely separate function to do this, or can I simply put both queries in the 1 php script and have the information from the first query be used for my 2nd query.

Any help is appreciated thanks!

Here is the jQuery function:

$(function() {

/*            $('#abbrev').val("");
*/             
            $("#q16_location16").autocomplete({
                source: "location_query.php",
                minLength: 1,
                select: function(event, ui) {
                    $('#q16_location161').val(ui.item.LocationID);
                    $('#SystemName').val(ui.item.SystemName);
                    $('#SiteAddress1').val(ui.item.SiteAddress1);
                    $('#SiteAddress2').val(ui.item.SiteAddress2);
                    $('#SiteCPP').val(ui.item.SiteCPP);
                    $('#Contact').val(ui.item.Contact);
                    $('#SiteLocationHours').val(ui.item.SiteLocationHours);
                }
            });
        });

and the php script:

/* If connection to database, run sql statement. */
if ($conn)
{

    $fetch = mysql_query("
    SELECT Location.LocationID,
    Location.SystemName,
    Location.SiteAddress1,
    Location.SiteAddress2,
    CONCAT_WS(' ', Location.SiteCity, Location.SiteProvince, Location.SitePostalCode) AS SiteCPP,
    CONCAT_WS(' ', Location.ContactName, Location.ContactPhone, Location.ContactEmail) AS Contact,
    Location.SiteLocationHours, 
    CONCAT_WS(' ', SystemName, SiteNameLocation, SiteAddress1, SiteCity, SiteProvince, SitePostalCode) as expr2 
    FROM Location 
    WHERE Location.SystemName like '%".mysql_real_escape_string($_GET['term'])."%'
    OR Location.SiteNameLocation like '%".mysql_real_escape_string($_GET['term'])."%'
    OR Location.SiteAddress1 like '%".mysql_real_escape_string($_GET['term'])."%'
    OR Location.SiteCity like '%".mysql_real_escape_string($_GET['term'])."%'
    OR Location.SiteProvince like '%".mysql_real_escape_string($_GET['term'])."%'
    OR Location.SitePostalCode like '%".mysql_real_escape_string($_GET['term'])."% '
    LIMIT 0,15");


    /* Retrieve and store in array the results of the query.*/
    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
        $row_array['LocationID'] = $row['LocationID'];
        $row_array['value'] = $row['expr2'];
        $row_array['SystemName'] = $row['SystemName'];
        $row_array['SiteAddress1'] = $row['SiteAddress1'];
        $row_array['SiteAddress2'] = $row['SiteAddress2'];
        $row_array['SiteCPP'] = $row['SiteCPP'];
        $row_array['Contact'] = $row['Contact'];
        $row_array['SiteLocationHours'] = $row['SiteLocationHours'];

        array_push($return_arr,$row_array);
    }
}

/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($return_arr, $return_arr2);

So when the user types in "New York" they can can select that option. In my example New York has an ID of 5. I also have a query that selects different streets in new york but this is in a separate table. in my streets table however, there is a "LocationID" column that for every street in new york will have a value of 5. So I want to take that ID of 5 when a user enters in new york and generate all the streets from a different table which also have that ID. I have tried multiple things in terms of creating a new function but I am just unsure of how I would pass that ID to the function.

Thanks

You can use one PHP script for this. Here's about what I'd think the basic structure will look like:

  1. Pass two values to "location_query.php". The first value would be the name of the table that you want to query. The second value could be the selection result from the auto-complete text box.
  2. Create a prepared statement in "location_query.php" from the two values that were passed to "location_query.php".
  3. Perform your query.
  4. JSON encode the result (just like you did before).

I'd also like to point out a security concern with your code. You should be using Mysqli and prepared statements instead of PHP's MySQL and mysql_real_escape_string. mysql_real_escape_string has been shown to have security deficiencies that can lead to security breaches and PHP's MySQL class has been deprecated. Mysqli and Prepared statements are much safer, and, in my opinion, provide for cleaner code since it allows for the separation of the SQL and the parameters.

Hope this helps!

EDIT: I think I understand what you're trying to do now, but I think there's a better way to go about doing it. Instead of assigning the id value to a hidden field and trying to have jquery detect every time that field is changed, I would just do the following:

For your first text box's select method:

select:function(event, ui) {
    $.get("location_query.php", {
        searchterm:$(ui).val()
        }, yourFunction);
}

Here's an example implementation of "queryFinished":

function queryFinished(data, textStatus, jqXHR) {
    var mJSON = $.parseJSON(data);
    /* mJSON is the parsed JSON data returned from your "location_query.php" 
       script.*/
    //TODO the rest of your code
}

Here's what's going on:

  1. We define a custom function to be called when the first text box has a new item selected. This functions only purpose is to call a GET on "location_query.php".
  2. Then, we pass the value of the selected field from the first text box via our GET call.
  3. We then create a function to be called when GET returns.
  4. Finally, we parse the encoded JSON that is returned by "location_query.php". After that, you can perform whatever tasks you need with the parsed JSON (mJSON in our example).

Taking this approach keeps us from having to worry about "listening" for a value change in our hidden ID field and makes everything nice and clean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM