EllisLab text mark
Advanced Search
     
Make an select with php and ajax
Posted: 09 October 2012 04:32 PM   [ Ignore ]
Joined: 2012-08-24
17 posts

Hi i have this problem to load a select of countries i have this code:

JS

function CargarPaises () {
  

elegido
=$("#pais").val();
$.
post("Panel_Admin/Traer_Paises"{ elegidoelegido }, function(data){
$("#ciudad").html(data);
      
}); 

Controller

public function Traer_Paises($id_region)
      
{
$paises
=$this->Admin_Model->Traer_Paises($id_region);
        
foreach (
$paises as $pais{
 
echo "<option> $pais->pais </option>";
 

And the result is nothing dont load nothing in the select.

 

 
Posted: 09 October 2012 08:00 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

POSTed values aren’t automatically passed to a method like url parameters, so your Traer_Paises() method isn’t receiving $id_region.  You need to grab that from $this->input->post(‘elegido’), since that’s how you are sending your data in your ajax call…

 Signature 
 
Posted: 09 October 2012 11:14 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-10-09
1 posts

I think in the JS

//pais is a select html
var elegido=$("#pais option:selected").val();
//and then, de post send de option selectec value =D 

in the controller

//you must load library 'input'
$local_var_sel $this->input->post('elegido'); 
 
Posted: 10 October 2012 07:44 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-06-09
50 posts
public function Traer_Paises()
{
   $id_region 
$this->input->post('elegido');
   
$paises=$this->Admin_Model->Traer_Paises($id_region);
   
$options '';
   foreach (
$paises as $key=>$pais
   
{
     $options
.="<option value=$key>$pais->pais</option>";
   
}  
   
echo $options;

Hope this will help you. If you still face problem make sure your Database is returning value.

 Signature 

Anbu