JQuery Ajax POST in Codeigniter
$(document).ready(function(){ $("#send").click(function() { $.ajax({ type: "POST", url: base_url + "chat/post_action", data: {textbox: $("#textbox").val()}, dataType: "text", cache:false, success: function(data){ alert(data); //as a debugging message. } });// you have missed this bracket return false; }); });
In codeigniter there is no need to sennd "data" in ajax post method..here is the example .
searchThis = 'This text will be search'; $.ajax({ type: "POST", url: "<?php echo site_url();?>/software/search/"+searchThis, dataType: "html", success:function(data){ alert(data); }, });
Note : in url , software is the controller name , search is the function name and searchThis is the variable that i m sending.
Here is the controller.
public function search(){ $search = $this->uri->segment(3); echo '<p>'.$search.'</p>'; }
I hope you can get idea for your work .
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class UserController extends CI_Controller { public function verifyUser() { $userName = $_POST['userName']; $userPassword = $_POST['userPassword']; $status = array("STATUS"=>"false"); if($userName=='admin' && $userPassword=='admin'){ $status = array("STATUS"=>"true"); } echo json_encode ($status) ; } }function makeAjaxCall(){ $.ajax({ type: "post", url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser", cache: false, data: $('#userForm').serialize(), success: function(json){ try{ var obj = jQuery.parseJSON(json); alert( obj['STATUS']); }catch(e) { alert('Exception while request..'); } }, error: function(){ alert('Error while request..'); } });}