dsj2222222
dsj2222222
2015-12-26 11:57
浏览 52

在codeigniter中调用Ajax

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???

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsuishou8039
    dongsuishou8039 2015-12-26 12:54
    已采纳

    There is one mistakes in your code.

    Change

    url: 'url', // in ajax call
    

    To

    url: url,  // do not use quotes with url variable
    
    点赞 评论

相关推荐