is there a way to pass a variable to a codeigniter controller using jquery's load() function?
currently this is my non working code
main.php
<input type="hidden" id="url" name="url" value="<?php echo base_url();?>site/pax_dropdown/" />
<input type="hidden" val="2" id="value">
<ul id="result" class="dropdown-menu">
<?php $this->load->view("site/pax_dropdown"); ?>
</ul>
pax_dropdown.php
<li><a href="">3</a></li>
<li><a href="">4</a></li>
<li><a href="">5</a></li>
<li><a href="">6</a></li>
<li><a href="">7</a></li>
<li><a href="">8</a></li>
<li><a href="">9</a></li>
<?php
echo $id;
?>
script.js
var url = $("#url").val();
var val = $("#value").val();
$('#result').load(url,val);
controller
public function pax_dropdown($id)
{
$data['id'] = $id;
$this->load->vars($data);
$this->load->view("site/pax_dropdown");
}
with this code the pax_dropdown.php successfully loads inside the
<ul id="result">
in my main.php however my test variable $id cannot be found and says Message: Undefined variable: id am i doing something wrong?
by the way i also tried sending the variable to the controller this way:
main
<input type="hidden" id="url" name="url" value="<?php echo base_url();?>site/pax_dropdown/2" />
i placed the variable to be passed at the end of the url, which in this case is "2"
and it still did not work
Look at this example to pass a variable using jQuery.load()
:
var siteName='example.com';
$("#result").load(sitename+"/site/pax_dropdown/2", {"variableName": variable});
Try this:
$(function(){
var url = $("#url").val();
var val = $("#value").val();
$("#result").load(url, {id: val});
});
function pax_dropdown()
{
$data['id'] = $this->input->post('id');
$this->load->vars($data);
$this->load->view("site/pax_dropdown");
}
And
<ul id="result" class="dropdown-menu">
<?php //$this->load->view("site/pax_dropdown"); comment out this line ?>
</ul>
Don't you have a default ID? If so, set this to the $id. If you don't want any default number, just wrap it into an if statement (if (isset($id)) and don't use the $id if it isn't set and logically shouldn't be set.
Maybe the logic behind the code would help as well for finding a solution.