I'm new to PHP and having some trouble calling PHP methods in through jQuery.
I'm using CodeIgniter. The method I want to call is in /application/controllers/home.php
. Here's the relevant code:
<?php
class Home extends CI_Controller {
function __construct() {
// Call the Controller constructor
parent::__construct();
...
}
function addToCart() {
// code to add to cart
}
The method I'm attempting to call is addToCart
.
Here's the jQuery code:
<script>
$(document).ready(function() {
$(".addToCart").on('click', function () {
var url = "<?= base_url() ?>home/addToCart";
console.log('here');
$.ajax({
url: url,
data: {'id': 1},
type: 'POST'
});
return false;
});
});
</script>
Even though the path is correct, I keep getting a 404.
I can't seem to pin-point what is going wrong. Thank you for your help!
Fixed:
The Home
controller was not the default controller for the app. I had to add index.php
in front of it.