简体   繁体   English

通过Ajax Jquery填充选择列表菜单

[英]Populate Select list Menu though Ajax Jquery

Before anybody says this is a duplicate of this and that question, let me assure you I have tried the solutions there and I have failed. 在有人说这是该问题的重复之前,让我向您保证,我在那儿尝试了解决方案,但失败了。 I am using a solution offered in this website to come up with my solution and I believe I am 90% done except for one error. 我正在使用网站提供的解决方案来提出我的解决方案,我相信我已完成90%的工作,除了一个错误。 I want to display a list of all codes that have a certain common ID associated with them. 我想显示具有与之关联的特定公共ID的所有代码的列表。

Here is my PHP code that I am using to get a list of codes 这是我用来获取代码列表的PHP代码

<?php
$budgetcode=$_POST['budgetcode'];
//$budgetcode=2102;

$selectcodes="SELECT * FROM tblbudget_codes WHERE T1 = $budgetcode";
$query=$connection->query($selectcodes);
$count=$query->num_rows;
if($count < 1)
{
    die('0');
}
else{
    while($row=$query->fetch_array()){

        $T1=($row['T1']);
        $T2=($row['T2']);
        $T3=($row['T3']);
        $T4=($row['T4']);
        $optionValue = $T1."-".$T2."-".$T3."-".$T4;
            echo json_encode("<option>$optionValue</option");            
        // echo json_encode('1');
        }

    }
 ?>

Here is the ajax call i am using to fetch the codes 这是我用来获取代码的ajax调用

$.post("Functions/getbudgetcodes.php",{budgetcode:budgetid},function(data){
    if(data!='0')
       { 
       $("#budgetcode").html(data).show();
       $("#result").html('');
        }   
        else{
             $("#result").html('<em>No codes found. Contact Administrator</em>');
            }
        },'json')
    //alert(budgetid);

    })

The problem here is that jquery does not understand the data it is receiving if it is not numeric. 这里的问题是,如果jquery不是数字,它不会理解它正在接收的数据。 Eg if I comment out the json_encode('1') and put random html code instead of data in my success part, I get results displayed in my browser. 例如,如果我注释掉json_encode('1')并在我的成功部分中放入随机的html代码而不是数据,我会在浏览器中显示结果。 Can anybody tell me why jquery is only recognizing numeric values that are being echoed from PHP and not varchar values. 谁能告诉我为什么jquery只识别从PHP回显的数字值,而不识别varchar值。 Using jquery 1.4.2. 使用jQuery 1.4.2。 Any help appreciated. 任何帮助赞赏。

EDIT 编辑
I have managed upto some point and now i am stuck. 我设法达到了某种程度,现在我被困住了。 I have used John's Answer and here is my jquery code. 我使用了约翰的答案,这是我的jquery代码。 i just need to split the array and append each element to a variable one at a time like here 我只需要拆分数组并将每个元素一次追加到一个变量中,就像这里一样

here is the code. 这是代码。 Somebody please tell how I split (data). 有人请告诉我如何拆分(数据)。 i can alert it but it is comma seperated. 我可以提醒它,但它是逗号分隔的。 Just need to get the individual items append them to variable html and then display it. 只需要将各个项目附加到变量html然后显示它。

$.post("Functions/getbudgetcodes.php",{budgetcode:budgetid},function(data){
     if(!$.isEmptyObject(data))
       { 
       //alert (data);
      // alert(split (data))
     var html = '';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        html += '<option>' +data+ '</option>';
    }
       $("#budgetcode").html(html).show();
       $("#result").html('');
        }   
        else{
             $("#result").html('<em>No codes found. Contact Administrator</em>');
            }
        },'json')

I would skip JSON altogether: 我会完全跳过JSON:

PHP PHP

echo "<option>$optionValue</option>";

Everything else should work. 其他一切都应该有效。

Finally figured it out. 终于想通了。 Here is the php code 这是PHP代码

$selectcodes="SELECT * FROM tblbudget_codes WHERE T1 = $budgetcode";
$query=$connection->query($selectcodes);
$count=$query->num_rows;
if($count < 1)
{
    die('0');
}
else{
    while($row=$query->fetch_array()){
        $data[] = $row;

                }   
    echo json_encode($data);        
    }
 ?> 

Here is the jquery code 这是jQuery代码

$.post("Functions/getbudgetcodes.php",{budgetcode:budgetid},function(data){
     if(!$.isEmptyObject(data))
       { 
       //alert (data);

     var html = '';
     var joiner='';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        joiner=([data[i].T1,data[i].T2,data[i].T3, data[i].T4].join('-'));
        //alert(joiner);
        html += '<option>'+joiner+'</option>';
    }
       $("#budgetcode").html(html).show();
       $("#result").html('');
        }   
        else{
             $("#result").html('<em>No codes found. Contact Administrator</em>');
            }
        },'json')

Had to use join to join the multiple codes using a dash. 不得不使用连接来使用破折号加入多个代码。 Hope this helps. 希望这可以帮助。 The PHP part and part of the jquery was inspired by this question jQuery的的PHP部分和部分的灵感来自于这个问题

FWIW, for populating select lists I usually use the following jQuery code: FWIW,为了填充选择列表,我通常使用以下jQuery代码:

// populates select list from array of items given as objects: { name: 'text', value: 'value' } function populateSelectList(parent, items) { parent.options.length = 0; //从作为对象给出的项目数组中填充选择列表:{名称:'文本',值:'值'}函数populateSelectList(parent,items){parent.options.length = 0;

  if (parent.options.length === 0) { parent.options[0] = new Option("Please select something...", ""); } $.each(items, function (i) { if (typeof (this) == 'undefined') { return; } parent.options[el.options.length] = new Option(this.name, this.value); }); } 

and to call the above function i pass the results of an ajax call using the map method where #select is the selector for the parent select element. 并调用上述函数,我使用map方法传递了ajax调用的结果,其中#select是父select元素的选择器。 I am setting the Text property to the name and Value to the value but that should change according to the objects returned by the ajax call (eg assuming the returned objects have a Value and Text properties). 我正在将Text属性设置为名称,并将Value设置为该值,但是应该根据ajax调用返回的对象进行更改(例如,假设返回的对象具有Value和Text属性)。

populateSelectList($('#select').get(0), $.map(results, function (result) { return { name: result.Text, value: result.Value} })); populateSelectList($('#select')。get(0),$ .map(results,function(result){return {name:result.Text,value:result.Value}}));;

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

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