0

Here I want to fetch record and want to show in view with the use of ajax. Here I couldn't get the json data. And If I get, what will be the proper method to pass to the view. Through this, I want to get username and comments from the table and want to show in view on click event using ajax. I want to do like Facebook, when user comments, the comment shows without the page load.

Controller:

public function get_comments()
{

            $query=$this->db->query("SELECT user_name,comments FROM user_comments join user_reg where user_reg.user_id=user_comments.user_id");
            $temp = $query->result();
            foreach($temp as $row)
            {
                header('Content-Type: application/json');
                echo json_encode($row); 

            }
            exit();

}

View:

<form action="" method="post" name="usercomments" id="usercomments"> 
            <div>
                <textarea name="comment" form="usrform" placeholder="Enter comment here..." id="ucom"></textarea>
                </br>

                <div class="tab-group">
                <input type="submit" class="button button-block tab" id="submitbutton" value="Comment"/>
                </div>
            </div>

        </form> 

    $(document).ready(function()
    {

        $("#submitbutton").click(function(event)
        {
            //alert('hiii');
            event.preventDefault();

            jQuery.ajax({
                type:"POST",
                url:"<?php echo base_url();?>index.php/welcome/get_comments",
                dataType:"json",
                data:"",
                success:function(data)
                {
                    console.log(data);
                    alert(data);
                } 


            });



        });

    });
Viral
  • 33
  • 5
  • What happens when you submit the form? Do you get any errors in console? Do you get any serverside error in response? There isnt enough information here. – wrxsti Jul 13 '16 at 13:22
  • When I submit the form, the records are inserted but I couldn't fetch them. There is no any error and also I could not get info in console too. – Viral Jul 13 '16 at 13:24

2 Answers2

1

Try something along these lines. We are rebuilding the rows into a more easily readable clientside object, and encoding it outside of the loop. Also you don't need to set the headers like that.

$query=$this->db->query("SELECT user_name,comments FROM user_comments join user_reg where user_reg.user_id=user_comments.user_id");
$temp = $query->result();
$response = array();
foreach($temp as $row){
   $user = $row['user_name'];
   $response[$user][] = $row['user_comments'];
}
echo json_encode($response); 
wrxsti
  • 3,434
  • 1
  • 18
  • 30
0

You should try to put your "echo json_encode" outside of your loop.

Use an array to store yours rows and json_encode this array.

J.Lgl
  • 93
  • 1
  • 11
  • Thnks @J.Lgl but how can I get in my view? – Viral Jul 13 '16 at 13:40
  • You can watch this topic http://stackoverflow.com/questions/21040794/how-to-access-json-encoded-data-of-an-array-using-javascript to see how to use your data with jQuery. I can't see any html structure for your comments in your view, there is just the form to post one. Check this doc to manipulate the DOM http://www.w3schools.com/jquery/jquery_dom_add.asp – J.Lgl Jul 13 '16 at 14:32