0

I have been aggrivated with this problem for two days now and I've spent enough time trying to figure it out myself.

I currently have two pages, one page has a form on it where an address field is being autocompleted by fetching a value from a json_encode provided by another PHP page. My problem is, I need to get other information from that page and I can't for the life of me figure out how to get it.

The autocomplete jQuery on my form page is:

jQuery(document).ready(function(){
        $('#address').autocomplete({source:'suggest_address.php', minLength:3});
    });

That jQuery is using jQuery UI Autocomplete 1.8.2 to operate.

The suggest_address.PHP page that is creating the json:

<?php

if ( !isset($_REQUEST['term']) )
exit;

$dblink = mysql_connect('x', 'y', 'z') or die( mysql_error() );
mysql_select_db('x');

$rs = mysql_query('select address, mapname from abcd where address like "%'. mysql_real_escape_string($_REQUEST['term']) .'%" limit 0,6', $dblink);

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $getdate = mysql_query('SELECT city, lpday, lpmonth, lpyear FROM abcd WHERE map="'.$row['mapname'].'"');
        $lpday = mysql_result($getdate, 0, "lpday");
        $lpmonth = mysql_result($getdate, 0, "lpmonth");
        $lpyear = mysql_result($getdate, 0, "lpyear");
        $city = mysql_result($getdate, 0, "city");
        $zip = substr($row['mapname'], 0,5);
        $data[] = array(
            'label' => $row['address'].' | '. $row['mapname'] . ' | ' . $lpmonth . '/' . $lpday . '/' . $lpyear ,
            'value' => $row['address'],
            'city' => $city,
            'zip' => $zip
        );
    }
}

echo json_encode($data);
flush();

I need to get 'city' and 'zip' and make jquery on my form page auto insert those values to the city and zip fields on the form. It is currently only doing this with the address.

I have tried a lot of things but this is the last failure that I tried:

$.get('suggest_address.php', function(returned_data) {$('#city').val(returned_data.city);$('#zip').val(returned_data.zip);},'json');

Sorry if this is a poor explanation, if I need to be clearer let me know. All help is appreciated.

RugerSR9
  • 448
  • 1
  • 4
  • 17
  • returned_data is an array not an object. – chf Jul 09 '14 at 15:46
  • Perhaps [this example](http://jqueryui.com/autocomplete/#remote-jsonp) will help? – Blazemonger Jul 09 '14 at 15:47
  • you'd be better off using a join rather than repeatedly querying the db – andrew Jul 09 '14 at 15:48
  • @Blazemonger I already have the autocompletion working fine, but when someone clicks on the option they want, it needs to also bring the zip and city values back to the form page rather than just the address. – RugerSR9 Jul 09 '14 at 15:50

1 Answers1

2

Almost, your missing the select event in your autocomplete.

So something like:

<script>
jQuery(document).ready(function(){
    $( "#address" ).autocomplete({
        source: "suggest_address.php",
        minLength: 3,
        select: function( event, ui ) {
            $('#city').val(ui.item.city);
            $('#zip').val(ui.item.zip);
        }
    });
});
</script>

Obligatory suggestion, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106