Ajax data not being sent to controller function (CodeIgniter)
Ajax data not being sent to controller function (CodeIgniter)
I have an anchor tag and would like its data-id
to be sent to a function in the controller which would in turn retrieved data from the database through the model.
However the data is not getting past the controller. The ajax response is showing that the data was sent but controller shows otherwise.
data-id
Here is my ajax code:
$(document).on("click",".learn-more",function()
var sub_item_id = $(this).data("id");
$.ajax(
url:"<?php echo base_url();?>Designs/business_cards",
type:"POST",
data:sub_item_id:sub_item_id,
success:function(data)
console.log(data);
,
error: function(error)
throw new Error('Did not work');
)
);
I had set datatype:"json" but the data was not being sent so I removed the datatype and it worked,the ajax part that is.Or atleast the response showed that data was sent.
My controller code is:
function business_cards()
$id = $this->input->post('sub_item_id');
$data['quantity'] = $this->subproduct_model->get_quantities($id);
$this->load->view('category/business-cards',$data);
My model code is:
public function get_quantities($sub_item_id)
$this->db->select('quantities');
$this->db->where('id',$sub_item_id);
$query = $this->db->get('sub_products');
return $query->result_array();
HTML Code which includes the anchor tag
<?php foreach ($results as $object):?>
<a href="#" data-toggle="modal" data-id="<?=$object['id'];?>" data-target="#pricelist-modal-2" class="learn-more">View Prices</a>
<?php endforeach?>
The data-id
is displaying the correct value as per the iteration.
data-id
When I check the result array of the model code it is an empty array showing that the $sub_item_id
was not passed in the controller. What could be the problem?
$sub_item_id
@MohitKumar when i add sub_item_id i get an error message
– lorrainemutheu
Aug 31 at 15:35
what did you try to do to troubleshoot this? please do a
echo
of the value of $this->input->post('sub_item_id');
in business_cards()
function, what does ajax success console.log()
print? can you include the relevant html as well?– 95faf8e76605e973
Aug 31 at 17:07
echo
$this->input->post('sub_item_id');
business_cards()
console.log()
@emineminems the error message I am referring to is the one in the
ajax error function
. When I echo
$this->input->post('sub_item_id')
nothing is displayed.– lorrainemutheu
Aug 31 at 17:33
ajax error function
echo
$this->input->post('sub_item_id')
Or could the problem be that I did not use
dataType:json
in my ajax
– lorrainemutheu
Aug 31 at 19:19
dataType:json
ajax
2 Answers
2
I just copied your code and I was able to get the value in the controller.
In your controller function do var_dump($id). Then in your developer tools (F12) check the console. Since you have console.log(data) that var_dump should be in the console. It won't show on the screen.
Some other things to check:
Does your db have records with that ID? Could your db result array be empty because it actually should be?
Are you sure that the data-id actually has a value when you click the tag?
I was checking the var_dump on the screen and not in the console. I have seen it is returning a result in the console
array(1) [0]=> array(1) ["quantities"]=>string(28) "200,500,1000,2000,5000,10000"
but when i loop through the array in my view using foreach($quantity as $object) echo quantity['quantities']
I get no result. Is it a must I get my result in the success function of the ajax or what could be wrong with my loop??– lorrainemutheu
Sep 1 at 7:34
array(1) [0]=> array(1) ["quantities"]=>string(28) "200,500,1000,2000,5000,10000"
foreach($quantity as $object) echo quantity['quantities']
@lorrainemutheu Yes you must use the success function. Since your using Ajax your view is not being reloaded. So you can either return the array and use append() or html() which are jQuery functions, or you could create a new view and return that to your Ajax from your controller then use html() to put it where you need it on you main view.
– Colby Boren
Sep 2 at 17:10
it is not passed to the controller because you forgot to put a parameter inside the function of your controller.
Note: you cannot use input post because you're not using form.
function business_cards($id) //put a parameter here, serve as container of your passed variable from **ajax**
//$id = $this->input->post('sub_item_id');
$data['quantity'] = $this->subproduct_model->get_quantities($id); //pass the id to your model
$this->load->view('category/business-cards',$data);
change your ajax code to this..
$(document).on("click",".learn-more",function()
var sub_item_id = $(this).data("id");
$.ajax(
url:"<?php echo base_url('Designs/business_cards/"+sub_item_id+"');?>", //pass the id here
type:"POST",
success:function(data)
console.log(data);
,
error: function(error)
throw new Error('Did not work');
)
);
When I did this I got
undefined index $id
error notice on $this->subproduct_model->get_quantities($id)
and missing parameter
in function business_cards($id)
– lorrainemutheu
Sep 1 at 7:41
undefined index $id
$this->subproduct_model->get_quantities($id)
missing parameter
function business_cards($id)
make sure that the id is not empty before passing it to your ajax.
– curiosity
Sep 1 at 7:48
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
use this url:"<?php echo base_url();?>Designs/business_cards/+sub_item_id",
– Mohit Kumar
Aug 31 at 15:24