【问题标题】:Select input with values from mysql database使用 mysql 数据库中的值选择输入
【发布时间】:2014-03-13 19:29:39
【问题描述】:

我想使用this jquery plugin 从数据库中获取值...

我创建 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>

和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;
?>

当我运行 php 代码时,我得到了 json:

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

问题不在于 php 代码...我的 html/jquery 代码有什么问题?

我什么都没有,我无法从 json.php 文件中获取值

更新:

我发现错误是 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>

【问题讨论】:

  • 我也尝试过:
    但我还是没有从数据库中获取值
  • 如果您使用chrome,请右键单击->检查元素->网络选项卡并刷新页面并查看ajax的请求和响应
  • 我更新了我的代码,但现在我无法选择它,因为当我选择并单击然后值只是 dessapear

标签: javascript php jquery mysql json


【解决方案1】:

您需要返回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};
        }
    }
});

您可以在此处查看演示:http://jsfiddle.net/huseyinbabal/68fD2/1/。在演示中,我使用了本地数据,但它适用于您上面的 ajax 代码。

编辑:

如果你想在你的演示中这样做,你可以使用以下;

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
});

这是一个工作演示:http://jsfiddle.net/huseyinbabal/68fD2/6/

【讨论】:

  • 是的,但请尝试,agroagro.com/select2/select.html 当我选择价值然后消失,不要得到它
  • 如何选择多个?如何保存数据,为什么我的数据在我放松时消失了
  • 让我看看你提供的演示,我会修复它
  • @gmaestro 你的输入被隐藏了,并且多选有相同的id,你能把多选的id更新为test_multiple吗?那么我们可以继续
  • @gmaestro 我已经修好了。请参阅我对Edit 部分的更新答案
猜你喜欢
  • 2015-02-10
  • 2015-10-19
  • 2018-09-23
  • 2014-03-27
  • 1970-01-01
  • 2021-05-14
  • 2011-03-24
  • 1970-01-01
  • 2013-12-15
相关资源
最近更新 更多