Seach

delete update using jquery in codeigniter

note create a js in codeigniter foler and add jquery to it
create a database database
load url,form_validation,form to auto load.php
set database.php

Form.php(Controller)

<?php
defined('BASEPATH') OR EXIT('Dirrect Access Not Allowed');
class Form_model extends CI_Model
{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function add($data)
    {
     return  $this->db->insert('exp_registeration',$data);
     
       
    }
    public function show()
    {
        $query=$this->db->query("SELECT * FROM  exp_registeration");
        return $query->result();
    }
    public function remove($id)
    {
       $this->db->wehre('id',$id);
       $this->db->delete("exp_registeration");
    }
    public function update($data)
    {
        $update_data=array('name'=>$data[0],'age'=>$data[1],'emailid'=>$data[2],'phoneno'=>$data[3],'address'=>$data[4]);
$this->db->where('id',$data[5]);
$this->db->update("exp_registeration",$update_data);
    }
   }?>


form_view.php(view file)

<link href="<?php echo base_url()."/css/style.css"; ?>" rel="stylesheet">
<form method="post" action="<?php echo site_url('Form/insert'); ?>">
    <table border="1" id="table_form">
        <tr>
            <td>
                <label>Name</label>
            </td>
            <td>
                <input type="text" name="student_name" value="<?php echo set_value('student_name');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Age</label>
            </td>
            <td>
                <input type="text" name="age" value="<?php echo set_value('age');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Email id</label>
            </td>
            <td>
                <input type="text" name="email_id" value="<?php echo set_value('email_id');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Phone Number</label>
            </td>
            <td>
                <input type="text" name="phone_number" value="<?php echo set_value('phone_number');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Address</label>
            </td>
            <td>
                <input type="text" name="address"value="<?php echo set_value('address');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Password</label>
            </td>
            <td>
                <input type="text" name="password" value="<?php echo set_value('password');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <label>Retype Password</label>
            </td>
            <td>
                <input type="text" name="rpassword" value="<?php echo set_value('rpassword');?>">
            </td>
        </tr>
       
        <tr>
            <td>
                <input type="submit" name="save" value="Save">
            </td>
            <td>
                <input type="submit" name="show" value="Show">
            </td>
        </tr>
    </table>

</form>
<?php
echo validation_errors();
?>
<?php
if(isset($_REQUEST['show']))
{
    redirect('Form/show');
}
?>


Display_student_data.php(view)

<header>
<!--    <script src="../../js/jquery.js" type="text/javascript"></script>-->
    <script src="<?php echo base_url()."js/jquery.js"?>"></script>
</header>
<form method="post" id="form1">
   
   
    <table border="1">
        <th>Name</th>
        <th>Age</th>
        <th>Email</th>
        <th>Phone NO</th>
        <th>Address</th>
        <th>Operation</th>
<?php
$i=1;
foreach ($result as $data)
{
 
 ?>
        <tr>
            <td><input type="text" name="name" value="<?php echo $data->name;?>" id=<?php echo "name".$i;?>></td>
            <td><input type="text" name="name" value="<?php echo $data->age;?>" id=<?php echo "age".$i;?>></td>
            <td><input type="text" name="name" value="<?php echo $data->emailid;?>" id=<?php echo "email".$i;?>></td>
            <td><input type="text" name="name" value="<?php echo $data->phoneno;?>" id="<?php echo "phone".$i;?>"></td>
            <td><input type="text" name="name" value="<?php echo $data->address;?>" id="<?php echo "address".$i;?>"></td>
            <td><a href="<?php echo site_url()."/Form/edit/".$data->id; ?>" id="edit<?php echo $i;?>">Edit</a>&nbsp;&nbsp;&nbsp;<a href="<?php echo site_url()."/Form/delete/".$data->id;?>" id="delete">Delete</a></td>
        </tr>      
<?php
$i++;
}
?>
    </table>
</form>
<script>
    $(document).ready(function(e)   
    {
      
    limit=<?php echo $i-1;?>;
   
       $('a').click(function(e){
    choice=this.id.substr(0,this.id.length-1);
   
           id_=this.id.slice(-1);
           entryno=this.href.slice(-1);
          
           e.preventDefault();
           if(choice=="edit")
           {
           name=$("#name"+id_).val();
           age=$("#age"+id_).val();
           email=$("#email"+id_).val();
           phone=$("#phone"+id_).val();
           address=$("#address"+id_).val();
           data=name+"/"+age+"/"+email+"/"+phone+"/"+address+"/"+entryno;
           $.ajax({
                    url:'edit',
                    type:'post',
                    processData: false,
                    data:'data='+data,
                    success: function (data, textStatus, jqXHR) {
                        alert('Edited');
                    },
                 
                    });
                }
                else
                {
                   $.ajax({
                       url:'delete',
                       type:'post',
                       processData: false,
                       data:'id='+entryno,
                   });
                }
    });
   
    });
</script>

Form_model.php(model)

<?php
defined('BASEPATH') OR EXIT('Dirrect Access Not Allowed');
class Form_model extends CI_Model
{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function add($data)
    {
     return  $this->db->insert('exp_registeration',$data);
     
       
    }
    public function show()
    {
        $query=$this->db->query("SELECT * FROM  exp_registeration");
        return $query->result();
    }
    public function remove($id)
    {
       $this->db->wehre('id',$id);
       $this->db->delete("exp_registeration");
    }
    public function update($data)
    {
        $update_data=array('name'=>$data[0],'age'=>$data[1],'emailid'=>$data[2],'phoneno'=>$data[3],'address'=>$data[4]);
$this->db->where('id',$data[5]);
$this->db->update("exp_registeration",$update_data);
    }
   
   
}


?>

table structure

-- phpMyAdmin SQL Dump
-- version 3.5.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 02, 2015 at 03:50 PM
-- Server version: 5.5.24-log
-- PHP Version: 5.3.13

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `database`
--

-- --------------------------------------------------------

--
-- Table structure for table `exp_registeration`
--

CREATE TABLE IF NOT EXISTS `exp_registeration` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `age` int(11) NOT NULL,
  `emailid` varchar(300) NOT NULL,
  `phoneno` int(11) NOT NULL,
  `address` varchar(300) NOT NULL,
  `password` varchar(400) NOT NULL,
  PRIMARY KEY (`emailid`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `exp_registeration`
--

INSERT INTO `exp_registeration` (`id`, `name`, `age`, `emailid`, `phoneno`, `address`, `password`) VALUES
(2, 'hithin', 25, 'hh@gmail.com', 2147483647, 'ekn njarakal', 'c8383b5122dfc6cbf715d84167656d39'),
(3, 'abcf', 25, 'hhiitthhiinn@gmail.com', 2147483647, 'kerela ernakulam njarakal', 'e2fc714c4727ee9395f324cd2e7f331f');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;