Welcome Guest, Not a member yet? Register   Sign In
Make an select with php and ajax
#1

[eluser]Diegosf93[/eluser]
Hi i have this problem to load a select of countries i have this code:

JS
Code:
function CargarPaises () {
  

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

Controller
Code:
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.

#2

[eluser]CroNiX[/eluser]
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...
#3

[eluser]Unknown[/eluser]
I think in the JS
Code:
//pais is a select html
var elegido=$("#pais option:selected").val();
//and then, de post send de option selectec value =D
in the controller
Code:
//you must load library 'input'
$local_var_sel = $this->input->post('elegido');
#4

[eluser]Techno Heart[/eluser]
Code:
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.




Theme © iAndrew 2016 - Forum software by © MyBB