Skip to main content

Ajax Tutorials in Codeigniter

Ajax Implementation in Codeigniter
Ajax means Asynchronous Javascript and XML, it is used to partially update the content of a web page excluding complete web form.

Ajax improves the performance of web application and provide dynamic programming approach without reloading and submit the data.


for example, if we create a registration form and we want to check that email already exists when user switches to next text field then we can create ajax code to check the email id.


when we select country then the state list and when we select the state then the city list will be populated by AJAX.


when we search from Facebook or Google search box then data will be populated when we press any key that will be managed by Ajax.




Role of JavaScript or Jquery:-

It is used to handle the event in Ajax Process, which means if we select a dropdown list and display data into another dropdown list will be managed by AJAX.


Role of XML:-
XML means Extensible markup language which is used to transfer data from client machine(Browser) to server machine (Apache Server).

XMLHttpRequest:-  this class is used to send data from browser to web server.

XMLHttpResponse:-  It is used to get web server response using XML form.


AJAX EXAMPLE IN CI?
.....................................................................................................
Create Controller:-
<?php
class AjaxExample extends CI_Controller
{
   function __construct()
   {
    parent::__construct();
    $this->load->model('stumodel');
   }
   function index()
   {
    $this->load->view('ajaxview');
   }

   function searchdata($id)
   {
   
      $r = $this->stumodel->ajaxsearch($id);
      if(count($r)>0)
      $data['res']=$this->stumodel->ajaxsearch($id);
      else
      $data['res']="no record found";
      $this->load->view('searchview',$data);
   }

}








?>


..................................
View File:-

ajaxview.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
  function showdata(a)
  {
  if(a!='')
  {
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange=function()
  {
  document.getElementById("res").innerHTML=xmlhttp.responseText;
  }
  xmlhttp.open("POST","<?php echo site_url(); ?>/ajaxexample/searchdata/"+a,true);
  xmlhttp.send();
  }
           
  }


</script>
</head>
<body>
<center>

<h1>Search Content</h1>
<input type="text" name="txt" onkeyup="showdata(this.value)"  />

<div id="res"></div>
</center>

</body>
</html>
...........................................................
searchview.php
........................................................................
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<?php

   //print_r($res);
   if(is_array($res))
   {
   foreach ($res as $x) {
    foreach ($x as $y)
    {
    echo $y," ";
    }
    echo "<hr>";
   }

   }
   else
    echo $res;
?>
</body>
</html>

.............................................................................................................
Model:-

<?php
class Stumodel extends CI_Model
{
    function __construct()
    {
    parent::__construct();
    $this->load->database();
    }

 
    function ajaxsearch($id)
    {
        $res = $this->db->select('*')->from('student')->where("name LIKE '$id%'")->get()->result_array();
        return $res;
    }

}





?>






Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...