Wie zu tun, ein dynamisches dropdown mit Ajax in Codeigniter

Ich weiß, das mag sein [insertLongNumber]th mal jemand diese Frage, ich habe meine Forschung, aber ich kann nicht finden, eine Lösung, die passt zu meinem problem. So ist es hier.

Arbeite ich an einem dynamischen dropdown mit php und ajax, codeigniter. Ich bin neu in CI, und ich habe Grundkenntnisse in Ajax.

Was mir bisher aufgefallen ist, dass in der Konsole, es ist nicht zu erkennen, den Wert aus dem ersten dropdown, so bekomme ich departamento_id : undefined
Das macht mir die Sache problem kommt von ajax-Skript (ich habe es von der web)

Meiner Sicht, mit der ajax-code enthalten

<?php
$this->load->helper('html'); 
?>

<html>
<head>
    <title>Buscador</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#dpto-dropdown select').change(function () {
                var selDpto = $(this).attr('value');
                console.log(selDpto);
                $.ajax({
                    url: "test/ajax_call",
                    async: false,
                    type: "POST",
                    data: "departamento_id="+selDpto,
                    dataType: "html",

                    success: function(data) {
                        $('#ciudad').html(data);
                    }
                })
            });
        });
    </script>
</head>

<?php echo form_open('test/buscar');?>

<?php 

<div id='dpto-dropdown'><?php print form_dropdown('departamentos', $departamento) ?></div>


<div id="ciudad"><select><option value=''>-</option></select></div>
//rest of code...

Dies ist mein Controller-code:

class Test extends CI_Controller 
{
function __construct()
{
    parent::__construct();
    $this->load->model('buscador_model');
}   

function index()
{
    $departamentos = $this->buscador_model->traerInfoDptos();
    $precios = $this->buscador_model->traerPrecioHoteles();

    foreach($departamentos as $departamento){
        $dpto_final[$departamento->id] = $departamento->nom_departamento;
    }

    $info = array(
        'departamento' => $dpto_final,
        'precios' => $precios,
    ); 

    $this->load->view('buscador_view', $info);
}

function ajax_call()
{
    //check to see people wont go directly
    if (isset($_POST) && isset($_POST['departamento_id'])) 
    {
        $dpto = $_POST['departamento_id'];
        $ciudad = $this->buscador_model->traerCiudadPorDpto($dpto);

        foreach ($ciudad as $c)
        {
            $ciudadfinal[$c->cod_ciudad] = $c->nom_ciudad;
        }

        //dropdown

        echo form_dropdown('Ciudades', $ciudadfinal);
    }
    else 
    {
        redirect('index');
    }
}
}

dies ist mein Modell:

Class Buscador_model extends CI_Model
{

function traerInfoDptos()
{
    $this->db->select('id, nom_departamento');
    $this->db->from('departamento');
    $query = $this->db->get();

    if ($query->num_rows > 0)
    {
        return $query->result();
    }
}

function traerCiudadPorDpto($dpto)
{
    $query = $this->db->query("SELECT nom_ciudad, cod_ciudad FROM ciudad WHERE departamento_id = '{$dpto}'");

    if ($query->num_rows > 0)
    {
        return $query->result();
    }
}

}//end buscador model class

InformationsquelleAutor Ant100 | 2013-03-11

Schreibe einen Kommentar