Selectize使用AJAX在选项中加载PHP数组

时间:2016-09-02 06:54:21

标签: javascript php jquery ajax selectize.js

我似乎无法从Selectize的选项中找到如何获取PHP查询的结果。我是使用AJAX的新手,所以请耐心等待:)

当手动加载选项并输入电子邮件地址时,结果可见:

                options: [
                {email: 'test@testcom'}
            ],

现在我需要相同的结果,但动态。我需要做什么才能将PHP数组放入该选项字段中?我使用json_encode将其转换为JSON。是否可以从php变量创建一个javascript变量?我希望这些信息足够,如果不是,请告诉我需要添加的内容。

这是我为Selectize创建的代码:

            $('.emailsearchreceiver').selectize({
            persist: false,
            maxItems: null,
            valueField: 'email',
            searchField: ['email'],
            options: [{email: 'test@testcom'}],
            render: {
                item: function(item, escape) {
                    return '<div>' +
                        (item.email ? '<span class="email">' + escape(item.email) + '</span>' : '') +
                        '</div>';
                },
                option: function(item, escape) {
                    var caption = item.email;
                    return '<div>' +
                        (caption ? '<span class="caption">' + escape(caption) + '</span>' : '') +
                        '</div>';
                }
            },
            createFilter: function(input) {
                var match, regex;

                // email@address.com
                regex = new RegExp('^' + REGEX_EMAIL + '$', 'i');
                match = input.match(regex);
                if (match) return !this.options.hasOwnProperty(match[0]);

                // name <email@address.com>
                regex = new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i');
                match = input.match(regex);
                if (match) return !this.options.hasOwnProperty(match[2]);

                return false;
            },
            create: function(input) {
                if ((new RegExp('^' + REGEX_EMAIL + '$', 'i')).test(input)) {
                    return {email: input};
                }
                var match = input.match(new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i'));
                if (match) {
                    return {
                        email : match[2],
                        name  : $.trim(match[1])
                    };
                }
                alert('Invalid email address.');
                return false;
            }
        });

这是我为检索电子邮件地址而创建的代码:

$query = $this->Transfer->query( "SELECT DISTINCT emailaddresses.emailaddress
                                  FROM transfers 
                                  JOIN emailaddresses ON (emailaddresses.transfer_id = transfers.transfer_id AND emailaddresses.type <> 'SENDER')
                                  WHERE transfers.created_user_id = $user_id " );

if(!$query)
{
    //die connection
}

//place all email addresses in an empty array
foreach ( $query as $results => $r )
{
    $emailaddresses[] = $r['emailaddresses']['emailaddress'];
}

json_encode($emailaddresses);

0 个答案:

没有答案