0

i actually want to use ajax in CodeIgniter to update value in the database. below is the code given:

views/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>

controller/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";

}


}

Its giving me the url and the id which i want to update in the database in the alert box...but its not updating the value in the database once the button is clicked.. Can anyone help???

devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    Check you browser console for exact issue. Is Ajax triggering or not. If you using Firebox use firebug addon it will help you to trigger ajax call. or put console error here. – Gaurav Srivastav Dec 26 '15 at 12:13
  • Also check the id in your controller function either coming or not – devpro Dec 26 '15 at 12:53

1 Answers1

1

There is one mistakes in your code.

Change

url: 'url', // in ajax call

To

url: url,  // do not use quotes with url variable
Ali Shan
  • 334
  • 3
  • 11
  • if your problem still persists, OR if you will face 403 error , then simply use CSRF with ajax. http://stackoverflow.com/questions/19333098/403-forbidden-error-when-making-an-ajax-post-request-in-django-framework – Ali Shan Dec 26 '15 at 12:56
  • Cool awesome like the way of debug – devpro Dec 26 '15 at 13:01
  • Thanks @Ali Shan..The above solution worked like a charm...i have been working on dis since last 24hrs..but could not find the bug...Thank u so much for making dis work.. – rinku simlai Dec 28 '15 at 04:55