user5876173
user5876173

Reputation:

while clicking on button it should be redirect to another page in ajax

i have one view requirements page in my application..in that i have one button called working..when user click on the button i changed the status in requirements table 0 to 1 by using ajax function..

So now the problem is when user click on the working button the button should be redirect to another page..

i tried a lot..but i couldn't do this..

Button:

<td><button id="<?php echo $idata->req_id; ?>" onClick="CallFunction(this.id)"  class="btn btn-info">working</button></td> 

Ajax function:

 function CallFunction(id)

{

  var url =  'http://127.0.0.1/job_portal/index.php/Requirements/change_status/';

        $.ajax({
        url: url,
        type: 'POST',
        data: {req_id: id},
        dataType: 'JSON',
        success: function(data) 
        {    
            if (data === true) {
                $('#button').text('FINISHED');
         }

            else
            {
              alert('error');
            }
        }
      });
}

</script>

I thought success function is not working..

Can anyone help me..how to do this..

Thanks in advance..

Upvotes: 1

Views: 5664

Answers (1)

user5139148
user5139148

Reputation:

Use following code to solve your problem

In Script File

function CallFunction(id) {
var url = 'http://localhost/job_portal/index.php/Requirements/change_status/';

    $.ajax({
    url: url,
    type: 'POST',
    data: {req_id: id},
    dataType: 'test',
    beforeSend: function(){ $("#"+id).html('please Wait...'); },
    success: function(data) 
    {  
        console.log(data);
        var obj = $.parseJSON(data)
        if(obj.status == 'success')
        {
          $("#"+id).html('Updated');
          window.location.href=window.location.href=obj.url;
        }
    },
    error:function(a,b,c)
    {
      console.log(c);
    }
  });

}

In Your Controller

public function change_status() 
{ 
    $req_id = $this->input->post('req_id'); 
    $this->RequirementModel->update_status($req_id); 
    $d = $this->RequirementModel->send_email($data['req_id']); 
    echo json_encode(array('status' => 'success', 'url' => base_url('Candidate/add_candidate')));  
    //echo true; 
    exit; 
}

In Your Model

function update_status($req_id) 
{ 
    $this->db->select('*'); 
    $this->db->from('requirements'); 
    $status = $this->db->query("update requirements set working_status='1' where req_id ='$req_id'"); 
    $data=array('working_status'=>$status); 
    $this->db->where('req_id',$req_id); 
    //$this->db->update('candidates_details',$data); 
    //$query=$this->db->get(); 
    //echo $this->db->last_query(); 
    //return $query->result(); 

}

In Your send_email Function

print_r($to); //-> comment this line

If you need more guideline on this issue post it we will sort it out

Upvotes: 1

Related Questions