1

i want to get data that i called it from controller and display it when the drop down menu changed this is my model:

$query = $this->db->query('select ifnull(max(content_id),0)+1 as content_id from news_contents where news_id = '.$news_id);
    return $query->result();

this is my controller:

 header('Content-Type: application/x-json; charset=utf-8');
 echo(json_encode($this->m_news_crud->new_newscontent_id($news_id)));

and this is my view:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
  <form>
   <select id="first">
    <option value="201512001">Option 1</option>
    <option value="201512002">Option 2</option>
    <option value="201512003">Option 3</option>
    <option value="201512004">Option 4</option>
   </select>
   <div id="msg"></div>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
 <script type="text/javascript">
 var b_url = "http://194.1.1.236/abuqir-arabic-master/"
 $('#first').change(function(e) {
        e.preventDefault();
        var news_id = $('#first').val();
        console.log(news_id);    
        $.ajax({
            type: 'get',
            url: (b_url + "c_news_crud/get_content_max/" + news_id),
            dataType: 'json',
            success: function(data) {
                alert(data);
            }
        });

        });

  </script>
  </body>
  </html>

Data structure is :

[{"content_id":"4"}]

it seems like it appear in browser developer tool put it doesn't appear in view

and finally sorry for my languages

4
  • did you get alert(data); ?? Commented Dec 15, 2015 at 9:25
  • the alert return [object Object] Commented Dec 15, 2015 at 9:30
  • Do alert (JSON.stringify(data)); Commented Dec 15, 2015 at 9:32
  • after you will get data you just need to append data to html. Simple. Commented Dec 15, 2015 at 9:33

1 Answer 1

1

You should append the data you get on success to the view, Try :

...
success: function(data) {
    $('#msg').text(data[0].content_id); //append 4 to the msg div
}
....

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

it return [object Object]
You have to show us the object returned structure, and what you want to show in view.
now after editing i wrote (success: function(data) { $('#msg').text(JSON.stringify(data)); }) and it return date like [{"content_id":"4"}] what i want is return number "4"

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.