0

I can't seem to find out how to get the results of my PHP query in the options from Selectize. I'm new to using AJAX, so bear with me :)

When the options are manually loaded and I'm typing in an email address, the results are visible:

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

Now i need the same result, but dynamically. What do I need to do to get my PHP array in that options field? I used json_encode to convert it into JSON. Is it possible to make a javascript variable from a php variable? I hope this information is sufficient, if not please tell me what i need to add.

This is the code I created for 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;
            }
        });

This is the code i created to retrieve email addresses:

$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);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2314339
  • 395
  • 1
  • 6
  • 16
  • http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – Andrej Sep 02 '16 at 07:03
  • Thx for the link. I tried the javascript directly method and i have the emailaddresses available in Selectize as Objects. Now i just need to get this array in options of Selectize so the emailaddresses will be visible. What function can i use? – user2314339 Sep 02 '16 at 08:23

0 Answers0