I'm new to all this AJAX thing so I thought that good learning will be to build simple TODO list. Below is index.php and corresponding controller. Index gets loaded without errors, but when I submit my task nothing is happening. Only page gets reloaded. Database is still empty.
index.php
<html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <h1>Todo</h1> <form id="add" > <input type="input" name="task" /> <input type="submit" value="Add" /><br /> </form> <script> $("form").submit(function() { var value = $("input:first").val(); $.ajax({ type: "POST", url: "<?php echo base_url(); ?>todo/add/" + $("input:first").val(), dataType: 'text', success: function() { var newP = $('<p />').text(value); $(".todos").append(newP).fadeIn(1000); } }); return true; }); </script> <div class="todos"></div> <p>Załadowano w <strong>{elapsed_time}</strong></p> </body> controller/todo.php
<?php class Todo extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('todo_model'); } public function index($type = NULL) { $this->load->view('todo/index'); } public function add($data) { $this->Todo_model->add($this->input->xss_clean($data)); } } ?> Update: todo_model.php:
<?php class Todo_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } public function add($data) { $this->db->insert('todo', $data); } public function get() { return $this->db->get('todo')->result(); } } ?>