在codeigniter中调用Ajax

时间:2015-12-26 11:57:42

标签: php ajax codeigniter

我实际上想在 CodeIgniter 中使用ajax来更新数据库中的值。 下面是给出的代码:

视图/ 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和id ...但是一旦点击按钮它就不会更新数据库中的值。 谁能帮忙???

1 个答案:

答案 0 :(得分:1)

您的代码中存在一个错误。

更改

url: 'url', // in ajax call

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