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);
                } 


            });



        });

    });
2
  • 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. Commented Jul 13, 2016 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. Commented Jul 13, 2016 at 13:24

2 Answers 2

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); 
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Thnks @J.Lgl but how can I get in my view?
You can watch this topic stackoverflow.com/questions/21040794/… 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 w3schools.com/jquery/jquery_dom_add.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.