I am encountering some challenges with ajax and codeigniter. Despite posting a previous question (link to question) and thinking I had resolved it, I am still facing issues. I am seeking assistance in creating simple code using ajax/codeigniter to increment a number inside a div/span upon clicking.
For the past few days, I have been attempting to achieve this, but I keep running into errors. My CodeIgniter settings are as follows:
base_url: localhost/test/
index: index.php
autoload: url
default controller: welcome (left unchanged for testing purposes)
I would greatly appreciate a straightforward example to implement this. I have tried again, but unfortunately, have not succeeded. Here is what I attempted this time:
Controller (welcome.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
function increase(){
$increase = $this->input->post('increase');
echo increase++;
}
}
JS (Ajax)
function increase(){
var number = parseInt($('#number').html()) + 1;
$.ajax({
type: 'POST',
url: 'localhost/test/welcome/increase',
data: { increase:number },
success:function(response){
$('#number').html(response);
}
});
}
View (HTML/CSS)
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery_v1.9.1.js"> </script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/script.js"> </script>
<style type="text/css">
#number {
display: block;
text-align: center;
width: 100px;
height: 30px;
margin: auto auto;
line-height: 30px;
border: 1px solid #999999;
border-radius: 5px;
}
</style>
</head>
<body>
<span id="number" onclick="increase()">0</span>
</body>
</html>
I am using the latest xampp on windows 7. The error I encounter when clicking on the span is:
POST http://localhost/test/localhost/test/welcome/increase 404 (Not Found)