jQuery的物品填充到一个选择使用jQuery AJAX JSON,PHP [英] jQuery populate items into a Select using jQuery ajax json, php
本文介绍了jQuery的物品填充到一个选择使用jQuery AJAX JSON,PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择字段。我必须填写与一个MySQL表采取的选项。
下面是一些小的PHP code我做了使用codeigniter框架
I have a select field. I must fill with options taken from a mysql table.
Here is some little php code I have done using codeigniter framework
$idcateg = trim($this->input->post('idcategory'));
$array1 = array(
'result' => $idcateg
);
echo json_encode($array1);
现在,jQuery的电话...
Now, the jQuery call...
$.post("<?=base_url()?>index.php/rubro/list_ajax/", {
'idcategory' : idc },
function(data){
alert(data.result);
}, "json");
在code正常工作。当我打电话后,我得到的类别ID作为一个结果。
现在,我应该修改code以上,所以我可以做的:
The code works fine. When I call the post, I get the categoryid as a result.
Now, I should modify the code above, so I can do:
- 后Ajax调用发送类别ID。这样做
- 在得到亚类这一类,并建立了阵列*
- json_en code数组和回声*
- 得到的结果早在jQuery的AJAX调用,去code和建设的&lt;选择>字段*
数组应与具有子阵列ID和名称,右各元素建? 非常感谢您的任何帮助。
The array should be built with each element having a sub-array with id and name, right? Thanks a lot in advance for any help
推荐答案
这是没有太大的不同。
$idcateg = trim($this->input->post('idcategory'));
$result = array();
$id = mysql_real_escape_string($idcateg);
$res = mysql_query("SELECT * FROM subcategories WHERE category = $id");
while ($row = mysql_fetch_array($res)) {
$result[] = array(
'id' => $row['subcatid'],
'desc' => $row['description'],
);
}
echo json_encode($result);
与
$.post("<?=base_url()?>index.php/rubro/list_ajax/", {
'idcategory' : idc },
function(data) {
var sel = $("#select");
sel.empty();
for (var i=0; i<data.length; i++) {
sel.append('<option value="' + data[i].id + '">' + data[i].desc + '</option>');
}
}, "json");
这篇关于jQuery的物品填充到一个选择使用jQuery AJAX JSON,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文