填充选择列表菜单虽然阿贾克斯jQuery的 [英] Populate Select list Menu though Ajax Jquery

查看:106
本文介绍了填充选择列表菜单虽然阿贾克斯jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有人说,这就是一个重复的和这个问题,让我向你保证,我已经试过了解决方案那里,我都失败了。我正在使用这网站拿出我的解决方案,我相信我是90%了,除了一个错误。我想显示所有codeS有一个与之相关的某些共同的ID列表。

下面是我用得到的codeS

列表我的PHP code

 < PHP
$预算code = $ _ POST ['预算code'];
// $预算code = 2102;

$选择codeS =SELECT * FROM tblbudget_ codeS其中t1 = $预算code;
$查询= $连接 - >查询($选择codeS);
$数= $查询 - > NUM_ROWS;
如果($计数< 1)
{
    死亡('0');
}
其他{
    而($行= $查询 - > fetch_array()){

        $ T1 =($行['T1']);
        $ T2 =($行['T2']);
        $ T3 =($行['T3']);
        $ T4 =($行['T4']);
        $ optionValue = $ T1 - $ T2 - $ T3 - $ T4。。。。;
            回声json_en code(<选项> $ optionValue< /选项);
        //回声json_en code('1');
        }

    }
 ?>
 

下面是Ajax调用我使用来获取codeS

  $岗位(功能/ getbudget codes.php,{预算code:budgetid},功能(数据){
    如果(数据!='0')
       {
       $(#预算code)HTML(数据).show();
       $(#结果)HTML('')。
        }
        其他{
             $(#结果)HTML('< EM>未发现codeS联系管理员< / EM>');
            }
        },JSON)
    //警报(budgetid);

    })
 

这里的问题是,jquery的不理解它正在接收,如果它不是数字数据。例如,如果我注释掉json_en code(1),并把数据随机HTML code,而不是在我成功的一部分,我在我的浏览器中显示的结果。谁能告诉我,为什么jQuery是只承认正在从PHP,而不是VARCHAR值回荡数值。使用jQuery 1.4.2。任何帮助AP preciated。

修改
我已经高达某一时刻管理,现在我卡住了。我已经使用了约翰的回答,这里是我的jQuery code。我只是需要分割该阵列并追加每个元素的变量逐个像<一个时间href="http://stackoverflow.com/questions/1502649/jquery-getjson-populate-select-menu-question">here

这里是code。有人请告诉我如何拆分(数据)。我可以提醒,但它是逗号分隔。只需要得到各个项追加到变量的HTML,然后显示出来。

  $岗位(功能/ getbudget codes.php,{预算code:budgetid},功能(数据){
     如果(!$。isEmptyObject(数据))
       {
       //警报(数据);
      //警报(分割(数据))
     VAR HTML ='';
    VAR的len = data.length;
    对于(VAR I = 0; I&LT; LEN;我++){
        HTML + ='&LT;选项&GT; +数据+'&LT; /选项&GT;';
    }
       $(#预算code)HTML(HTML).show();
       $(#结果)HTML('')。
        }
        其他{
             $(#结果)HTML('&LT; EM&GT;未发现codeS联系管理员&LT; / EM&GT;');
            }
        },JSON)
 

解决方案

终于想通了。这里是PHP code

  $选择codeS =SELECT * FROM tblbudget_ codeS其中t1 = $预算code;
$查询= $连接 - &GT;查询($选择codeS);
$数= $查询 - &GT; NUM_ROWS;
如果($计数&LT; 1)
{
    死亡('0');
}
其他{
    而($行= $查询 - &GT; fetch_array()){
        $数据[] = $行;

                }
    回声json_en code($的数据);
    }
 ?&GT;
 

下面是jQuery的code

  $岗位(功能/ getbudget codes.php,{预算code:budgetid},功能(数据){
     如果(!$。isEmptyObject(数据))
       {
       //警报(数据);

     VAR HTML ='';
     VAR木匠='';
    VAR的len = data.length;
    对于(VAR I = 0; I&LT; LEN;我++){
        木匠=([数据[I] .T1,数据[I] .T2,数据[I] .T3,数据[I] .T4]。加入(' - '));
        //警报(木匠);
        HTML + ='&LT;选项&GT;+木匠+'&LT; /选项&GT;';
    }
       $(#预算code)HTML(HTML).show();
       $(#结果)HTML('')。
        }
        其他{
             $(#结果)HTML('&LT; EM&GT;未发现codeS联系管理员&LT; / EM&GT;');
            }
        },JSON)
 

只好用用加入少许加入多个codeS。希望这可以帮助。 jQuery的的PHP部分,一部分灵感来自<一href="http://stackoverflow.com/questions/1502649/jquery-getjson-populate-select-menu-question">this问题

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. I want to display a list of all codes that have a certain common ID associated with them.

Here is my PHP code that I am using to get a list of codes

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

$.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. E.g 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. Can anybody tell me why jquery is only recognizing numeric values that are being echoed from PHP and not varchar values. Using 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. 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.

$.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')

解决方案

Finally figured it out. Here is the php code

$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

$.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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆