2015-12-26 4 views
0

Я действительно хочу использовать ajax в CodeIgniter для обновления значения в базе данных. ниже код Дано:Ajax call in codeigniter

просмотров/list_user.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Admin Panel</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/screen.css" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
     $('.round_button_circle').click(function() { 
    //Now just reference this button and change CSS 
    //$(this).css('background','green'); 
    var id=this.value;//Getting the id of the user to update the status from unpublish to publish 
    var url='<?php echo base_url(); ?>index.php/ajax_controller/user_status'; 
    alert(id+url); 
    $.ajax({ 
    type: 'POST', 
    url: 'url', //We are going to make the request to the method "list_dropdown" in the match controller 
    data: 'id='+id, //POST parameter to be sent with the tournament id 
    success: function(data) 
    { 

    } 
}); 

}); 
}); 
</script> 
</head> 
<body> 


    <div id="page-heading"> 
     <div class="button"><a href="create_category.php">Create User</a></div><!--create category--> 
     <h1>USERS</h1><!--users's list--> 
      </div> 
    <!-- end page-heading --> 
    <table border="0" style="width:97%;margin:0px auto" cellpadding="0" cellspacing="0" id="content-table"> 

    <tr> 
     <td> 
     <!-- start content-table-inner START --> 
     <div id="content-table-inner"> 
      <div class="stdiv"><div class="msg1"> 
       <?php 
       if(isset($_GET['msg'])) 
       { 
        $msg=$_GET['msg']; 
        if($msg=="usertype") 
        { 
         echo "User has been Created"; 
        } 
        if($msg=="create") 
        { 
         echo "User has been Created"; 
        } 
        if($msg=="delete") 
        { 
         echo "User has been Deleted"; 
        } 
        if($msg=="update") 
        { 
         echo "User has been Updated"; 
        } 
       } 
       ?> 
      </div> 

      </div> 
       <!-- start category-table --> 

       <table border="0" cellpadding="0" style="width:100%;margin:0px auto" cellspacing="0" id="product-table"> 
       <tr> 
        <tr> 
        <th class="tbl_hdr">USER ID</th> <!--ID-->      
        <th class="tbl_hdr">‫NAME</th> <!--Name-->          
        <th class="tbl_hdr">EMAIL</th> <!--Email--> 
        <th class="tbl_hdr">PHONE NUMBER‎</th> <!--ph number--> 
        <th class="tbl_hdr">STATUS</th> <!--publish/unpublish--> 
        <th class="tbl_hdr" colspan="2">ACTION</th> <!--edit/delte-->              
        </tr> 
        <?php foreach($users as $user){?> 
        <tr> 
        <td><?php echo $user->id;?></td> 
        <td><?php echo $user->username;?></td> 
        <td><?php echo $user->email;?></td> 
        <td><?php echo $user->contact;?></td> 
        <td ><button id="status" class="round_button_circle" value="<?php echo $user->id;?>"></button></td> 
        </tr>  
        <?php }?> 
       </table> 
       <!-- end product-table................................... --> 

      </div> 
      <!-- end content-table --> 
      <!-- start paging..................................................... --> 
         <!-- end paging................ --> 
      <div class="clear"></div> 

     <!-- end content-table-inner ............................................END --> 
     </td>  
    </tr> 
    </table> 
    <div class="clear">&nbsp;</div> 
</div> 
<!-- end content --> 
<div class="clear">&nbsp;</div> 
</div> 
<!-- end content-outer........................................................END --> 
<div class="clear">&nbsp;</div> 
<!-- start footer -->   
</body> 
</html> 

контроллер/ajax_controller.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Ajax_Controller extends CI_Controller { 

// Show view Page 


// This function call from AJAX 
public function user_status() 
{ 
$id=$this->input->post('id'); 
$data = array(
       'status' => 1 

      ); 
$this->db->where('id', $id); 
$this->db->update('tbl_users', $data); 
echo "1"; 

} 


} 

Его дает мне URL и идентификатор, который я хочу для обновления в базе данных в окне предупреждения ... но не обновлять значение в базе данных после нажатия кнопки. Может ли кто-нибудь помочь ???

+1

Check вы консольный браузер для точного выпуска. Вызывается Ajax или нет. Если вы используете Firebox, используйте firebug addon, это поможет вам вызвать вызов ajax. или введите здесь ошибку консоли. –

+0

Также проверьте идентификатор в вашей функции контроллера либо приходящий, либо не – devpro

ответ

1

В коде есть одна ошибка.

Изменить

url: 'url', // in ajax call 

Для

url: url, // do not use quotes with url variable 
+0

, если ваша проблема по-прежнему сохраняется, ИЛИ если вы столкнулись с ошибкой 403, то просто используйте CSRF с ajax. http://stackoverflow.com/questions/19333098/403-forbidden-error-when-making-an-ajax-post-request-in-django-framework –

+0

Прохладный, как способ отладки – devpro

+0

Спасибо @Ali Shan .. Это решение работало как шарм ... я работал над этим с последних 24 часов .. но не смог найти ошибку ... Спасибо, что так много для того, чтобы сделать эту работу. –