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 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥20 SQL server表计算问题
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)