Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从mysql数据库中选择输入值_Javascript_Php_Jquery_Mysql_Json - Fatal编程技术网

Javascript 从mysql数据库中选择输入值

Javascript 从mysql数据库中选择输入值,javascript,php,jquery,mysql,json,Javascript,Php,Jquery,Mysql,Json,我想使用从数据库中获取值 我创建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" /> <scri

我想使用从数据库中获取值

我创建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>
问题不在于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、文本对并使用以下结构

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



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

这是一个有效的演示:

我也尝试了:但是我也没有从数据库中获取值如果你使用chrome,右键单击->检查元素->网络选项卡并刷新页面,查看ajaxI更新我的代码的请求和响应,但现在我无法选择它,因为当我选择并单击时,然后值只是desapearies,但请尝试,当我选择“值”时,只需删除“梨”,不知道如何选择多个?如何保存数据,为什么我的数据会在我使用时丢失,请让我检查您提供的演示,我会修复it@gmaestro您的输入是隐藏的,并且multiple select具有相同的id,您能否将multipe select的id更新为test\u multiple?那我们就可以continue@gmaestro我已经修好了。有关编辑零件,请参见我的更新答案
<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>
<input type="hidden" name="test" id="test" style="width:200px;"/>



$('#test').select2({
    ajax: {
        dataType: "json",
        url: "json.php",
        results: function (data) {
            return {results: data};
        }
    }
});
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
});