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
Du musst angemeldet sein, um einen Kommentar abzugeben.
Finden Sie auf dieser Seite: http://www.onerutter.com/open-source/jquery/jquery-tips-how-to-get-value-of-selected-option-in-select-box.html
Müssen Sie
.val()
statt.attr('value')
könnte sein, was Sie bekommen, wenn Sie eine
console.log(data);
in Ihremsuccess
Rückruf?Ich dieses: `<html> <head> <meta name="author" content="Kai Oswald Seidler"> <meta http-equiv="cache-control" content="no-cache"> <title>XAMPP 1.7.7</title> <frameset rows="74," marginwidth="0" marginheight="0" frameborder="0" border="0" borderwidth="0"> <frame name="Kopf" src="head.php" scrolling=no> <frameset cols="150," marginwidth="0" marginheight="0" frameborder="0" border="0" borderwidth="0"> <frame name="navi" src="navi.php" scrolling=no> <frame name="Inhalt" src="start.php" marginwidth=20> </frameset> </frameset> </head> <body bgcolor=#ffffff> </body> </html>
halt es fest, es war ein Fehler mit der url "text/ajax_call". Ich lege den kompletten Pfad statt (localhost/../text/ajax_call). Vielen Dank für deine Hilfe @swatkins!
InformationsquelleAutor swatkins