0

I am using CodeIgniter, basically I have a table which has dummy data, and I am returning this data using jQuery.

I do this by:

Controller:

class Notifications extends CI_Controller{
        function getAlerts(){
            $query = $this->db->query("SELECT ID, subject, message, readMessage FROM data WHERE readMessage = 0");
            $result = $query->num_rows();

            if($result > 0){
                return $result;
                echo $result;
            }   
        }

Header Script:

<script type="text/javascript">
      $(function() {
        getMessageCount1();
      });
      function getMessageCount1() {
        $('div#alertsfrommydb').load('<?php echo base_url(); ?>notifications/getAlerts').fadeIn();
        setTimeout("getMessageCount1()", 10000);
      }
   </script>

and the view as such:

<div id="alertsfrommydb">
    </div>

I have a method which is called showAlerts() in the same controller which prints a table and shows my messages refreshing every 10 seconds, and this works perfectly fine using above approach.

In addition I have a method called notifications.php and I load this in my controller, which then allows me to simply in my view type:

print_r($this->notifications->getAlerts());

And the query shows my results fine which is '6' meaning I have 6 unread messages.

So my argument is, the query is working, so why will it not work using the jQuery approach.

Although my query works, I have a gut feeling there is something not right about it lol, hope someone can help.

Thanks

2
  • I don't see a ajax call anywhere in you code? Commented Feb 5, 2015 at 22:23
  • Erm the script in the header? If this is not ajax then my apologies, what would this be? @noob Commented Feb 5, 2015 at 22:30

1 Answer 1

2

No you are doing it wrong! the ajax call in codeigniter looks something like this:

function getMessageCount1() {

$.ajax({
     type: 'POST',
     url: '<?php echo base_url()."/notifications/getAlerts"?>',
     data: { NumberOfAlerts:result of your query }, 
     success:function(response){
        $('#alertsfrommydb').html(response);
     }
  });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks for the reply, I have now edited my post changing it from ajax to jQuery, FYI I have tried above approach and used the script to load ajax, still no luck :(
can you edit the original post with what you have tried with ajax
have already done that, I haven't tried anything with ajax, realised it was all jQuery @noob
I really appreciate your help and support, I've just tried what you suggested....... still no luck :.(
did you add json encode to the passed string ion notifications.php? like this $alerts =($this->notifications->getAlerts()); echo json_encode($alerts);

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.