CodeIgniter Forums

Full Version: dont want to refresh page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]hamzakhan[/eluser]
dont want to refresh page when delteing user by using ajax jquery.
how i can do that??
my controller
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    

    }
    
    function index()
    {
        $this->get_users();
    }
    
    function get_users()
    {
        $this->load->model('Mwelcome');

        $data['query'] = $this->Mwelcome->get_names();
        
        $this->load->view('welcome_message', $data);
    }    
    function delete()
    {            
      $id = $this->uri->segment(3, 0);  // get id
    // echo 'hamza testing'.$id;

     /*
     $id = $this->uri->segment(3, 0);  // get id
     $url = $_SERVER['QUERY_STRING'];
     */    
     $this->load->model('Mwelcome');
     $this->Mwelcome->delete_names($id);
    
    // echo 'Deleted Successfully';
     redirect('welcome/get_users');    
    
    // $this->get_users();
      // $this->load->view('welcome_message', $data);    
    }
    
    function add()
    {        
    
     $this->load->model('Mwelcome');
     $this->Mwelcome->add_name();
    
     redirect(index_page());    
    
      // $this->load->view('welcome_message', $data);    
    }
    
    function register()
    { // register user
        $this->load->view('register');
    }
    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
view
Code:
<?php
$counter = count($query);

echo "<table border=\"1\" align=\"center\" width=100%>";
echo "<tr><th> id </th>";
echo "<th> names </th></tr>";
$counter = 1;
foreach($query as $content)
{
    echo "<tr><td>";
    echo $counter;
    echo "</td><td>";
    echo  $content->name;    
    echo "</td><td>";
    //
    $att_anchor_dele = array('href'=>'#');
    //echo anchor('','Delete',$att_anchor_dele);
    echo '<a >id.')" >Delete</a>';
    
    echo "</td></tr>";
    $counter ++;
}
echo "</table>";

El Forum

[eluser]harman[/eluser]
First Remove
Code:
redirect('welcome/get_users');

Use Delete method with parameter

Code:
function delete($id){
   //your del code here
   if($isdelete==true){
      echo "sucess";
   }
   else{
      echo "ErrorCode";
  }
}


//Jquery
1. add jquery to your view.
2. use jquery Ajax Function


Code:
<a href="#" class="del">delete</a>

//add jquery here
//start script
Code:
$(function(){
          $(".del").click(function(){
                $.get('your_url/delete/&lt;?php echo $id;?&gt;', function(data) {
                    if(data=="sucess"){
                      alert('Delete performed.');
                     }
               });
          });
     });
//end script