简体   繁体   English

使用Codeigniter和Mysql显示Ajax结果时出错

[英]Error in displaying the ajax result using Codeigniter and Mysql

I just need a little help here about displaying my table query result. 我只需要一些有关显示表查询结果的帮助。 When i checked the response from my ajax I shows. 当我检查我的Ajax响应时,我显示了。 But when passing to the views it doesn't shows. 但是当传递到视图时,它不会显示。 Here's my code: 这是我的代码:

Controller/user.php 控制器/user.php

        <?php

        class User extends CI_Controller{

            public function __construct(){
                parent::__construct();
                $this->load->model('user_model');
            }

            public function index(){
                $this->load->view( 'index' );
            }

            public function read() {
                echo json_encode( $this->user_model->getAll() );
            }


        }

    ?>

Model/user_model.php 型号/user_model.php

        <?php

        class User_Model extends CI_Model{

            public function __construct(){
                parent::__construct();
            }

            public function getAll(){

                $qGetAllData = $this->db->get('user');
                if($qGetAllData->num_rows() > 0){
                    return $qGetAllData->result();
                }else{
                    return array();
                }

            }

        }

    ?>

    View/index.php

        <html>
      <head>
        <title><?php echo $title; ?></title>
        <base href="<?php echo base_url(); ?>" />
        <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
        <link type="text/css" rel="stylesheet" href="css/styles.css" />
      </head>

      <body>

        <table id="records"></table>

        <div id="tabs">

          <ul>
            <li><a href="#read">Read</a></li>
            <li><a href="#create">Create</a></li>
          </ul>

          <div id="read">
            <table id="records"></table>
          </div>

          <div id="create"></div>

        </div>




        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-templ.js"></script>

        <script type="text/template" id="readTemplate">
          <tr>
            <td>${id}</td>                  //doesn't display ID
            <td>${name}</td>                //doesn't display Name
            <td>${email}</td>               //doesn't display EMial
          </tr>
        </script>

        <script type="text/javascript" src="js/myjs.js"></script>

      </body>

    </html>

Javascript/myjs.js Javascript / myjs.js

            $(document).ready(function(){

        $( '#tabs' ).tabs({
                fx: { height: 'toggle', opacity: 'toggle' }
            });

            $.ajax({
                url: 'index.php/user/read',
                datatype: 'json',
                success: function(response){
                    $('#readTemplate').render(response).appendTo('#records');
                }
            });

        });

That's all guys. 伙计们。 I just don't know where's my error. 我只是不知道我的错误在哪里。

I think you have issue with just render data. 我认为您仅在渲染数据时遇到问题。

please review this link for reference. 请查看此链接以供参考。

Please try below code. 请尝试以下代码。

Your html : View/index.php
<html>
  <head>
    <title><?php echo $title; ?></title>
    <base href="<?php echo base_url(); ?>" />
    <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
    <link type="text/css" rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <table id="records"></table>
    <div id="tabs">
      <ul>
        <li><a href="#read">Read</a></li>
        <li><a href="#create">Create</a></li>
      </ul>
      <div id="read">
        <table id="records"></table>
      </div>
      <div id="create"></div>
    </div>
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-templ.js"></script>
    <script type="text/javascript" src="js/myjs.js"></script>
  </body>
</html>

Your JS : js/myjs.js
var readtpl = "<tr><td>${id}</td><td>${name}</td><td>${email}</td></tr>";
$.template( "readTemplate", readtpl );
 $(document).ready(function(){

    $( '#tabs' ).tabs({
            fx: { height: 'toggle', opacity: 'toggle' }
        });

        $.ajax({
            url: 'index.php/user/read',
            datatype: 'json',
            success: function(response){
                $.tmpl( "readTemplate", response ).appendTo( "#records" );
            }
        });

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM