King Goeks
King Goeks

Reputation: 512

jQuery autocomplete from php-json without database

I want to make autocomplete on my form with jQuery. The source taken from json data from php, but i don't use database. Here is my code :

facebookfriends.php

<?php
include('facebookdata.php');

$user_friend = $user_friends['data'];

$json_friends = json_encode($user_friend);
echo $json_friends;
?>

Script

$(function() {
  $( "#search" ).autocomplete(
  {
     source:'facebookfriends.php',
  });
});

JSON DATA

[{"name":"Indiarto Priadi","id":"502163984"},
 {"name":"Agustin Kertawijaya","id":"511990261"},
 {"name":"Jecklyne Christin L","id":"528197912"},
 {"name":"Jazi Eko Istiyanto","id":"531149275"},
 {"name":"Esritha Situmorang","id":"535864892"},
 {"name":"Christy Margaretha Siregar","id":"543468540"},
 {"name":"Daniel Oscar Baskoro","id":"549332828"},
 ........]

I just want to display the name in autocomplete to ensure that the autocomplete works well. But it doesn't. Please help. Thank you!

Upvotes: 0

Views: 760

Answers (2)

AAA
AAA

Reputation: 142

1.You have to parse json in php 2.make an array for dropdown list

Following steps to be followed:

     $data ='[{"name":"Indiarto Priadi","id":"502163984"},
              {"name":"Agustin Kertawijaya","id":"511990261"},
              {"name":"Jecklyne Christin L","id":"528197912"}, 
              {"name":"Jazi Eko Istiyanto","id":"531149275"},
              {"name":"Esritha Situmorang","id":"535864892"},
              {"name":"Christy Margaretha Siregar","id":"543468540"},
              {"name":"Daniel Oscar Baskoro","id":"549332828"}]';
    $user_friend =  json_decode($data, true );
    $data=array();
    foreach($user_friend as $key=>$val)
            $data[]=$val['name'];
    $json_friends =json_encode($data);
    echo $json_friends;

Upvotes: 1

Padmanathan J
Padmanathan J

Reputation: 4620

you need to pass label and value into you php file

For example

    $data[] = array(
                'label' =>  $row['city_name'].','.$state['state_name'].','.$c_name['country_name'],
                'value' =>  $row['city_name'].','.$state['state_name'].','.$c_name['country_name']
                    );

echo json_encode($data);
flush();

Upvotes: 0

Related Questions