Problem with dropdown & jquery - El Forum - 12-18-2009
[eluser]s.passiatore[/eluser]
Hi, I try to update three dropdown with ajax (jquery). The problem is that when I select a value from second combo, the third is updated but the second return void (not with the selected value)
Here the code:
CONTROLLER:
Code: <?php
class Indicatori extends Controller {
function Indicatori()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
}
function index()
{
echo '.';
}
function parametri()
{
$this->output->enable_profiler(TRUE);
$this->load->model('Parametri','',TRUE);
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('selparametri');
}
function prova()
{
$this->load->database();
if (isset($_POST['indicatore1']) && $_POST['indicatore1']!='')
{
$options ='';
echo $options.= '<option value="001" class="dynamic">1value1</option>';
echo $options.= '<option value="001" class="dynamic">1value2</option>';
echo $options.= '<option value="001" class="dynamic">1value3</option>';
}
if (isset($_POST['indicatore2']) && $_POST['indicatore2']!='')
{
$options ='';
echo $options.= '<option value="001" class="dynamic">2value1</option>';
echo $options.= '<option value="001" class="dynamic">2value2</option>';
echo $options.= '<option value="001" class="dynamic">2value3</option>';
}
}
}
/* End of file indicatori.php */
/* Location: ./system_osp/application/controllers/indicatori.php */
VIEW
Code: <html>
<head>
<title>OSP - Selezione parametri</title>
[removed][removed]
[removed]
function test(step)
{
var valore = $("#indicatore" + step).val();
$.ajax({
url: "<?=base_url();?>index.php/indicatori/prova",
global: false,
type: "POST",
async: false,
dataType: "html",
data: "indicatore" + step + "="+ valore, //the name of the $_POST variable and its value
success: function (response) //'response' is the output provided by the controller method prova()
{
//counts the number of dynamically generated options
var dynamic_options = $("*").index( $('.dynamic')[0] );
//removes previously dynamically generated options if they exists (not equal to 0)
if ( dynamic_options != (-1)) $(".dynamic").remove();
$("#indicatore" + (step + 1)).show();
$("#indicatore" + (step + 1)).append(response);
}
});
return false;
}
[removed]
</head>
<body>
<h1>OSP - Selezione parametri</h1>
<?php
$attributes = array('class' => 'parametri', 'id' => 'parametri');
echo form_open('indicatori/parametri', $attributes);
$options = array();
$query = $this->db->query('SELECT codice, descrizione FROM indicatori');
foreach ($query->result_array() as $row)
{
$options[$row['codice']] = $row['descrizione'];
}
$js = 'id="indicatore1" onChange="test(1)"';
echo form_dropdown('indicatore1', $options,'TRUE',$js);
$options = array("000" => "Selezionare");
$js = 'style="display: none" id="indicatore2" onChange="test(2)"';
echo form_dropdown('indicatore2', $options,'TRUE',$js);
$options = array("000" => "Selezionare");
$js = 'style="display: none" id="indicatore3" onChange="test(3)"';
echo form_dropdown('indicatore3', $options,'TRUE',$js);
echo form_close();
?>
</body>
</html>
Thanks a lot!
Stefano
Problem with dropdown & jquery - El Forum - 12-18-2009
[eluser]Ben Edmunds[/eluser]
Sorry I don't have time to go through all the code but try debugging with firebug and by alert()ing the step and the response.
|