-2

I am using MagicSuggest to autocomplete tags. This code works perfect:

    $(document).ready(function() {
        var ms3 = $('#ms3').magicSuggest({
            data: [{id:12,name:'php'},{id:1212,name:'java'},{id:112,name:'python'},{id:125,name:'html'},{id:172,name:'css'}]
        });
    });

But this one doesn't:

    $(document).ready(function() {
        var ms3 = $('#ms3').magicSuggest({
            data:'data.php'
        });
    });

php:

    <?php
          echo "[{id:12,name:'php'},{id:1212,name:'java'},{id:112,name:'python'},{id:125,name:'html'},{id:172,name:'css'}]";
    ?>

P.S. This works when data:'data.json' with json data inside.

Gad
  • 41,526
  • 13
  • 54
  • 78

3 Answers3

1

I reckon that the problem could be the fact that the php file returns wrong mime type. Try adding this at the top of your data.php file
header('Content-type: application/json');

So your data.php file would look like

<?php
   header('Content-type: application/json');
   echo "[{id:12,name:'php'},{id:1212,name:'java'},{id:112,name:'python'},{id:125,name:'html'},{id:172,name:'css'}]";
?>
Pankucins
  • 1,690
  • 1
  • 16
  • 25
1

In JSON specification not presend single quotes - you must change to double quotes

better you can use json_encode function

header('Content-type: application/json');
echo json_encode(array(
    array('id'=>12, 'name'=> 'php')
));
Svyatoslavik
  • 166
  • 1
  • 1
  • 7
0

Why dont you...

<?php
    $mydata = "[{id:12,name:'php'},{id:1212,name:'java'},{id:112,name:'python'},{id:125,name:'html'},{id:172,name:'css'}]";
?>

$(document).ready(function() {
    var ms3 = $('#ms3').magicSuggest({
        data: <?php echo$mydata;?>
    });
});

That way you can populate $mydata with data from a database query etc.

OR

Try the follwing in your data.php file;

<?php
    header('Content-type: application/json');
    echo "[{id:12,name:'php'},{id:1212,name:'java'},{id:112,name:'python'},{id:125,name:'html'},{id:172,name:'css'}]";
 ?>
Dom
  • 7,135
  • 1
  • 11
  • 13