开发者

Populate an associative select from a database

Here's my problem: I have two select fields in my web application.

The second one depends on the choice made on the first one. The first one is populated from my database with the classical:

<?php
    //connection to database in an include file
    $query = "SELECT DISTINCT platform 
                FROM Appversions";

    $res = mysql_query($query);
    while($row = mysql_fetch_array($res)) {
       echo "<option value=\"\"" . $row['platform'] . "</option>\n";
    }
?>

which is working just fine.

But then I need to populate my second select with a que开发者_JAVA技巧ry that would look like:

SELECT version 
  FROM Appversions 
 WHERE platform = <choice made in first select>;

I understand that I need to use JavaScript with an onChange function call to do that, but I can't figure out what that function should look like or how it will have access to my query result.


Usually i have this jquery code:

$('#select1').change(function(){
    if($(this).val() == ''){
        $('#select2').attr('disabled', 'true');
    }else{

        var select1 = $(this).val();
        $.getJSON('index.php',
                {
                    option: "com_spin",
                    controller: "guest",
                    task: "getProvincieByRegionId",
                    idRegione: idRegione
                },
                function(json){
                    $('#select2').html(json.html);

                }   
        );
        $('#select2').removeAttr('disabled');
    }
});

in php i have something like this (basically i return the html for the options:

    Zend_Loader::loadClass ( 'Zend_Json' );
    $idRegione = JRequest::getVar ( "idRegione" );
    $modelProvince = new Spin_lkpprovincia ();
    $provincie = $modelProvince->getElencoProvinciePerRegione ( $idRegione );
    $html = "<option value=''>Selezionare una voce</option>";
    foreach ( $provincie as $provincia ) {
        $html .= "<option value='" . $provincia ['idProvincia'] . "'>" . $provincia ['nome'] . "</option>";
    }
    $json = array (
                success => "OK", 
                html => $html );

    $json = Zend_Json::encode ( $json );
    echo $json;
    die ();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜