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.