javascript - 使用 mysql 数据库中的值选择输入

标签 javascript php jquery mysql json

我想使用 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>

最佳答案

您需要返回id, text 对并使用以下结构;

<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/

关于javascript - 使用 mysql 数据库中的值选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22388991/

相关文章:

Javascript ES6 - 类内部的枚举像静态枚举一样在外部使用

javascript - 使用代码拆分获取 block 时包含 header

javascript - 为什么 window.undefined 在旧浏览器中是可变的?

javascript - 这个 ES6 箭头函数和常规函数的区别?

php - 对于巨大的表格数据,使用 div 标签是否比使用 table tr 和 td 标签更快?

javascript - jQuery 文本加载动画

php - 使用 Woo Cancel for Customers Plugin 在我的帐户订单列表中添加取消按钮

php - Stripe : parameter_invalid_integer, PHP 新手

php - 当用户按下按钮时,如何从 View 中更改模型内的值?

jquery - XHR 不起作用,因为 "Origin is not allowed by Access-Control-Allow-Origin"