0

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!

3
  • tell me what's your issue. kindly explain clearly Commented Nov 29, 2013 at 4:55
  • the autocomplete does not appear (nothing happens when I type some letter in input fields) Commented Nov 29, 2013 at 4:59
  • Autocomplete needs either an array of strings (without keys), or an array with keys of label and/or value - api.jqueryui.com/autocomplete/#option-source. Can you update facebookdata.php to add a label key, which could just be a copy of your name value. Commented Nov 29, 2013 at 5:07

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.