简体   繁体   English

从mysql数据库中选择带有值的输入

[英]Select input with values from mysql database

I want to use this jquery plugin to get values from database... 我想使用这个jQuery插件从数据库中获取值...

I create jquery ajax code and HTML to get values from database: 我创建了jQuery ajax代码和HTML以从数据库获取值:

 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<link href="http://ivaynberg.github.com/select2/select2-3.3.2/select2.css" rel="stylesheet" type="text/css" />
<script src="http://ivaynberg.github.com/select2/select2-3.3.2/select2.js"></script>
</head>

<body>


<select id="test" style="width:200px;">
              <option value=""><option>

    </select>


        <script>
$('#test').select2({
    ajax: {
        dataType: "json",
        url: "json.php",
        results: function (data) {
            return {results: data};
        }
    }
});

</script>
  </body>

and json.php code: 和json.php代码:

<?php
$pdo=new PDO("mysql:dbname=ddd;host=localhost","ddd","ddd");
$statement=$pdo->prepare("SELECT id,ime_prezime FROM radnici");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo $json;
?>

when I run php code i get json: 当我运行php代码时,我得到json:

[{"id":"1","ime_prezime":"Pera Peric"}]

s the problem is not with php code... what is wrong in my html/jquery code? s问题不在于php代码...我的html / jquery代码出了什么问题?

I dont get anything, I cant fetch values from json.php file 我什么也没得到,我无法从json.php文件中获取值

UPDATE: 更新:

I find error is was json format, but now I cant save values that I get , so when I click values just disapear... 我发现错误是json格式,但是现在我无法保存得到的值,所以当我单击值时,这些值就消失了...

<input id="test" style="width:300px;">
<select multiple id="test" style="width:300px"></select>




        <script>
        function formatValues(data) {
    return data.ime_prezime;
}
$('#test').select2({
    ajax: {
        dataType: "json",
        url: "json.php",
        results: function (data) {
            return {results: data};
        }
    },
    formatResult: formatValues
});

</script>

You need to return id , text pair and use following structure; 您需要返回idtext对并使用以下结构;

<input type="hidden" name="test" id="test" style="width:200px;"/>



$('#test').select2({
    ajax: {
        dataType: "json",
        url: "json.php",
        results: function (data) {
            return {results: data};
        }
    }
});

You can see demo here: http://jsfiddle.net/huseyinbabal/68fD2/1/ . 您可以在此处查看演示: http : //jsfiddle.net/hus​​eyinbabal/68fD2/1/ In demo, I have used local data, but it works with your ajax code like above. 在演示中,我使用了本地数据,但是它可以与上述的ajax代码一起使用。

Edit: 编辑:

If you want to do that as in your demo, you can use following; 如果要像在演示中那样进行操作,可以使用以下命令;

function formatValues(data) {
    return data.ime_prezime;
}
var test = $('#test');
var data = [{"id":"1","ime_prezime":"Pera Peric"},
          {"id":"2","ime_prezime":"Something else"},
          {"id":"3","ime_prezime":"Lorem"},
          {"id":"4","ime_prezime":"Ipsum"}
         ];
$(test).select2({
    data:{results: data, text: 'ime_prezime'},
    width: "300px",
    formatResult: formatValues,
    formatSelection: formatValues,
    multiple: true
});

Here is a working demo: http://jsfiddle.net/huseyinbabal/68fD2/6/ 这是一个工作示例: http : //jsfiddle.net/hus​​eyinbabal/68fD2/6/

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

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