Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Combobox dinámico

Estas en el tema de Combobox dinámico en el foro de Frameworks JS en Foros del Web. Tengo el código que genera un combobox dinamico en php, los datos los obtiene de una tabla mysql. <?php function cargar_combo($tabla,$value,$opt){ $enlace = mysql_connect("localhost", "root", ...
  #1 (permalink)  
Antiguo 11/01/2006, 05:41
 
Fecha de Ingreso: diciembre-2002
Ubicación: Madrid
Mensajes: 378
Antigüedad: 21 años, 4 meses
Puntos: 11
Combobox dinámico

Tengo el código que genera un combobox dinamico en php, los datos los obtiene de una tabla mysql.

<?php
function cargar_combo($tabla,$value,$opt){
$enlace = mysql_connect("localhost", "root", "") or die("No pudo conectarse : " . mysql_error());
mysql_select_db("prueba") or die("No pudo seleccionarse la BD.");
$sql = "select * from ".$tabla;
$res = mysql_query($sql) or die (mysql_error());
echo "<select name='$tabla' class='botones'>";
while($fila = mysql_fetch_assoc($res)){
echo "<option value='$fila[$value]'>$fila[$opt]</option>";
}
echo "</select>";
mysql_close($enlace);
}
?>




Lo que quiero es que al pulsar en enlace2 se cargue en el combobox los datos de la tabla2, si pulso en el enlace3 se carguen los datos de la tabla3, etc ...

La cuestión es que no quiero que se recarge la página, me han dicho que con ajax es posible, ¿ alguno me puede ayudar ?

Muchas gracias.
  #2 (permalink)  
Antiguo 11/01/2006, 06:11
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años, 1 mes
Puntos: 17
Buenas

Recién empiezo yo tb con AJAX, y estuve usando la clase xajax

Si vas a usar esa clase, podrías usar el siguiente código, adaptando la función agregar a tus necesidades de MySQL
Código PHP:
<?php

require ('xajax.inc.php');

class 
myXajaxResponse extends xajaxResponse
{  
    function 
addCreateOption($sSelectId$sOptionText$sOptionValue)  
    {  
        
$this->addScript("addOption('".$sSelectId."','".$sOptionText."','".$sOptionValue."');");
    }

    function 
addCreateOptions($sSelectId$aOptions)
    {
        foreach(
$aOptions as $sOptionText => $sOptionValue)
        {
            
$this->addCreateOption($sSelectId$sOptionText$sOptionValue);
        }
    }
}

function 
agregar($tabla)
{
    
$objResponse = new myXajaxResponse();

    if (
$tabla == 1) {
        
$aOptions['One'] = 1;
        
$aOptions['Two'] = 2;
        
$aOptions['Three'] = 3;
    } else {
        
$aOptions['Four'] = 4;
        
$aOptions['Five'] = 5;
        
$aOptions['Six'] = 6;
    }
    
$objResponse->addCreateOptions("select1",$aOptions);        
    return 
$objResponse;
}


$xajax = new xajax(); 


$xajax->registerFunction("agregar");

$xajax->processRequests();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>xajax example</title>
    <?php $xajax->printJavascript('./');
    <
script language="javascript" type="text/javascript">
    function 
limpiarselect() {
        
oCntrl document.getElementById('select1');
        while(
oCntrl.length 0oCntrl.options[0]=null
    }
    function 
addOption(selectId,txt,val) {
        var 
objOption = new Option(txt,val);
        
document.getElementById(selectId).options.add(objOption);
    }
    
</script>
</head>
<body style="text-align:center;">
    <div id="div1" name="div1"> </div>
    <br/>
    
    <button onclick="limpiarselect();xajax_agregar(1)" >Click Me</button>
    <button onclick="limpiarselect();xajax_agregar(2)" >CLICK ME</button>
    <select id="select1" name="select1">
        <option value="black" selected="selected">Black</option>
        <option value="red">Red</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
    </select>
</body>
</html>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Tema Cerrado




La zona horaria es GMT -6. Ahora son las 20:18.