weixin_33699914 2018-08-20 01:11 采纳率: 0%
浏览 15

检索输入文本中的数据

I am using echo json_encode($data); to send an array back to jquery ajax. I want to get data from json and retrieve it on input text and I got this error like: The URI you submitted has disallowed characters. I don't know on how to pass data in model. Btw, I'm using codeigniter.

Once I click the edit button, a modal will appear. Please, help me guys!!

Button

<a onclick="edit_content_by_id('.$row->post_id.')" title="Edit"><span class="ti-pencil"></span>

**Model**
    public function GetContentById($id) {
    $query = $this->db->select('*')->from('cms_posts_tbl cpt')->
    join('cms_contents_tbl cct', 'cct.post_id = cpt.post_id')->
    join('cms_category_tbl ccat', 'ccat.post_category_id = cpt.post_category')->
    where('post_id', $id)->get();   

    foreach($query as $row) {
        $data = $row;
    }

    echo json_encode($data);
}

**jQuery/AJAX**
function edit_content_by_id(id) {
    var data = { content_id : id };
    $.ajax({
        type: 'POST', url: 'http://localhost:81/ci_sample/model/GetContentById('+ id +')',
        data: data, dataType: 'json',
        cache: false,
        success:function(data) {
            $('#modal_content').modal('show');
            $('#modal_content').find($('post_title')).val(data.post_title);
        }
    });
}
  • 写回答

1条回答 默认 最新

  • weixin_33730836 2018-08-20 04:42
    关注

    You can't directly call a model function on ajax call. First you have to create a controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class myController extends CI_Controller {
      public function fetchData() {
        $content_id =  $_POST['content_id '];
        if($content_id){
        /*
          Model_Name = > Name of the model that have GetContentById().
          Assume that Model_Name => Content_Loader 
        */
        //$this->load->model(Model_Name);
          $this->load->model('Content_Loader');
          $data = $this->Content_Loader->GetContentById($content_id);
        }
        echo json_encode ($data); 
      }
    }
    

    Change the GetContentById function like this :

    public function GetContentById($id) {
      $query = $this->db->select('*')->from('cms_posts_tbl cpt')->
      join('cms_contents_tbl cct', 'cct.post_id = cpt.post_id')->
      join('cms_category_tbl ccat', 'ccat.post_category_id = cpt.post_category')->
      where('post_id', $id)->get();   
    
      foreach($query as $row) {
          $data = $row;
      }
    
      return $data;
    }
    

    And finally your ajax call will be like this:

    **jQuery/AJAX**
    function edit_content_by_id(id) {
        $.ajax({
            type: 'POST', url: 'http://localhost:81/ci_sample/mycontroller/fetchData',
            data: { content_id : id }, dataType: 'json',
            cache: false,
            success:function(data) {
                 console.log(data);
                 var response_data = JSON.parse(data);
                $('#modal_content').modal('show');
                $('#modal_content').find('.post_title').val(response_data .post_title);
            }
        });
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区