How to collect collect data from Jquery ajax response to html?

DMS-KH

I'm using Codeigniter and Jquery ajax for calling data from mysql and send to html form.

Problem: I Don't know how to call data from res object.

Here is my contrller

public function select_post(){

        $data = $this->ads_m->get_post('ads',array('user_id'=>  $this->user->user_id()));
        IF($data){
            foreach($data as $row ){

                $response = array(
                    'name'=>$row->name,
                    'price'=>$row->price,
                    'addr'=>$row->des,
                    'img'=>$row->img,
                    'status'=>$row->status,
                );
                echo json_encode(array('res'=>$response));
            }
        }else{
            echo json_encode(array('res'=>FALSE));
        }
    }

And here is Model function

public function get_post($from, $where = False) {

        $this->db->select("*");
        $this->db->from($from);
        if ($where) {
            $this->db->where($where);
        }
        $q = $this->db->get();
        if($q->num_rows()>0){
            return $q->result();
        }
    }

And this is Ajax

<script type="text/javascript">
       $(document).ready(function () {
        $.ajax({
         url: '<?php echo base_url('ads/select_post'); ?>',
         data: {},
         dataType: "json",
         cache: false,
         success: function (data) {
         console.log(data.['res']['name']);
       }
    });

});

and I get the result as below

{"res":{"name":"dfasdfas","price":"0","addr":"sadfdfasdfasdf","img":"","status":"1"}}{"res":{"name":"Heng"
,"price":"0","addr":"asdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasd
fasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdfasdfasfasdfasdfasdf"
,"img":"","status":"1"}}{"res":{"name":"asdDasdA","price":"0","addr":"ASDasdASD","img":"","status":"1"
}}{"res":{"name":"asdfas","price":"0","addr":"dfasdf","img":"","status":"1"}}
Umesh Kolapkar

// First change the response public function select_post(){

    $data = $this->ads_m->get_post('ads',array('user_id'=>  $this->user->user_id()));
    IF($data){
        var $responseArray = [];
        foreach($data as $row ){

            $response = array(
                'name'=>$row->name,
                'price'=>$row->price,
                'addr'=>$row->des,
                'img'=>$row->img,
                'status'=>$row->status,
            );
            array_push($responseArray, $response);

        }
        echo json_encode($responseArray);
    }else{
        echo json_encode(array('res'=>FALSE));
    }
}

//Then used below code to get the response in ajax
$(document).ready(function () {
    $.ajax({
        url: '<?php echo base_url('ads/select_post'); ?>',
        data: {},
        dataType: "json",
        cache: false,
       success: function (data) {
          $.each(data, function(item) {
          console.log(item.name);
       });
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to collect data from loop

From Dev

How to collect unstructured data from a database?

From Dev

How to collect data from several input boxes?

From Dev

How to collect chart data

From Dev

Ajax data collect in code lines

From Dev

HTML - collect data with php from a textfield out of the post <form>

From Dev

Collect associated data from array

From Dev

Collect data from facebook pages

From Dev

How to collect data from attributes from child component Angular 2

From Dev

How to collect same word from string using javascript or jquery

From Dev

How can I collect this data from a div using Selenium and Python

From Dev

How to collect data from select tags into Django views

From Dev

How to collect data from a list into groups based on condition?

From Dev

How to collect different data from array or hash by ruby language?

From Dev

Excel: How to collect data from a single cell and place it in a table?

From Dev

How to collect async response using RxJava

From Dev

How to access ajax response data as html with javascript or jquery?

From Dev

Collect data with PHP from dynamically created inputs

From Dev

Collect data from a readable stream into a variable

From Dev

Collect data from another site using Javascript

From Dev

Android delay loop to collect data from the gyroscope

From Dev

Mysqli - PHP to collect data from database

From Dev

Collect multiple json objects from data attribute

From Dev

excel collect data rolling from month to month

From Dev

Displaying data from an Ajax response with Jquery

From Dev

jQuery: Post data from ajax response

From Dev

How to collect data python using BeautifulSoup

From Dev

How much data does Chrome really collect?

From Dev

How to force LeakCheck Delphi library to not collect data?

Related Related

  1. 1

    how to collect data from loop

  2. 2

    How to collect unstructured data from a database?

  3. 3

    How to collect data from several input boxes?

  4. 4

    How to collect chart data

  5. 5

    Ajax data collect in code lines

  6. 6

    HTML - collect data with php from a textfield out of the post <form>

  7. 7

    Collect associated data from array

  8. 8

    Collect data from facebook pages

  9. 9

    How to collect data from attributes from child component Angular 2

  10. 10

    How to collect same word from string using javascript or jquery

  11. 11

    How can I collect this data from a div using Selenium and Python

  12. 12

    How to collect data from select tags into Django views

  13. 13

    How to collect data from a list into groups based on condition?

  14. 14

    How to collect different data from array or hash by ruby language?

  15. 15

    Excel: How to collect data from a single cell and place it in a table?

  16. 16

    How to collect async response using RxJava

  17. 17

    How to access ajax response data as html with javascript or jquery?

  18. 18

    Collect data with PHP from dynamically created inputs

  19. 19

    Collect data from a readable stream into a variable

  20. 20

    Collect data from another site using Javascript

  21. 21

    Android delay loop to collect data from the gyroscope

  22. 22

    Mysqli - PHP to collect data from database

  23. 23

    Collect multiple json objects from data attribute

  24. 24

    excel collect data rolling from month to month

  25. 25

    Displaying data from an Ajax response with Jquery

  26. 26

    jQuery: Post data from ajax response

  27. 27

    How to collect data python using BeautifulSoup

  28. 28

    How much data does Chrome really collect?

  29. 29

    How to force LeakCheck Delphi library to not collect data?

HotTag

Archive