I'm having some issues with ajax and codeigniter. I've already posted another question (link to question) and I thought I solved it, but I did not so I`m asking someone to write simple code with ajax/codeigniter that will increase number inside div/span on click.
I`m trying last few days to do this, but constantly getting errors..My CI setting are :
base_url : localhost/test/
index:index.php
autoload:url
default controller:welcome (I left it so just for this test)
I would be more than happy to have simple example to do this. I tried also, again, but without any luck. Here's what I tried 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'm using latest xampp on windows 7. Error that I get when I click on span - POST http://localhost/test/localhost/test/welcome/increase 404 (Not Found)
In your ajax, don't use external link for your JS just use internal.
Make sure that you set your $config['base_url'] =
http://localhost/test/
in config.php