简体   繁体   English

php中的select2值

[英]select2 values from php

I really stuck with select2 and the tags mode. 我真的坚持使用select2和标签模式。 I want to let the user enter data as tags. 我想让用户输入数据作为标签。 Seperated by " " and ",". 以“”和“,”分隔。 No problem. 没问题。 I've got this. 我有这个

Now I want to gather the already available tags by a php file. 现在,我想通过php文件收集已经可用的标签。 I've already tried several snippets from google and here. 我已经尝试过Google和此处的一些摘要。 The php file returns a json_encode. php文件返回一个json_encode。 Does anybody see what I am doing wrong? 有人看到我在做什么错吗?

Here is the code: 这是代码:

  if($('#s2_tag_handler_receipient').length) {
            $('#s2_tag_handler_receipient').select2({

        tags: true,
        tokenSeparators: [",", " "],
        createSearchChoice: function (term, data) {

            if ($(data).filter(function () {
                return this.text.localeCompare(term) === 0;
            }).length === 0) {
                return {
                    id: term,
                    text: term
                };
            }
        },
        multiple: true,
        ajax: {
            url: "imapMailBox/autoCompleteReceipientsJson.php",
            dataType: "json",
            data: function (term, page) {
                return {
                    q: term
                };
            },
            results: function (data, page) {
                return {
                    results: data
                };
            }
        }       
            });
        }

This is the php file: 这是php文件:

<?php 
    $return[]=array('Paul','NotPaul');
    echo json_encode($return);
?>

Try with : 尝试:

<?php 
    $return=array('Paul','NotPaul');
    echo json_encode($return);
?>

This will give : ["Paul","NotPaul"] 这将给出: ["Paul","NotPaul"]

In your version it gave : [["Paul","NotPaul"]] 在您的版本中,它给出了: [["Paul","NotPaul"]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM