1

I have create a notification system using php and jquery

I have a page name select-task.php this page contain this code

$sql = mysqli_query($conn,"SELECT 
count(db_taskid) as sum from tbl_task where db_read='0' and (db_userid='$uid' or db_transfered='$uid')")or die(mysqli_error($conn));
$row = mysqli_fetch_array($sql);
$count = mysqli_num_rows($sql);
echo $taskCount=$row['sum'];
$conn->close();

jquery code:

$(document).ready(function(){
$("#task").load("select-task.php");
setInterval(function(){
$("#task").load('select-task.php')
}, 20000);
 });

This code work fine and i receive the notification but i try to get the variable $taskCount from select-task.php page to do a verification if this variable equal to zero don't add class not if not add the class not

this is the html code

    <li class="dropdown dropdown dropdown-extended dropdown-notification dropdown-dark " id="header_notification_bar">
 <a class="dropdown-toggle" data-toggle="dropdown" href="#" data-hover="dropdown" data-close-others="true">Task <i class="icon-bell"></i><span class="badge badge-success" id="span"><div id="task"></div></span>
  <span class="caret"></span></a>
    <ul class="dropdown-menu">
    <li><a href="add-tasks.php" target="_blank">Add Task</a>
      <li><a href="all-tasks.php" target="_blank">All Task</a></li>
      </ul>

I've try to do like this

if($taskCount==0){echo'<span class="badge badge-success">';}
else{echo'<span class="badge badge-success not">';}

But i receive an error that this an unknown variable.

How can i get this error to do this verification ??!!!

0

2 Answers 2

1

You can use of this code for have a condition in your code:

<script>
$(document).ready(function(){
$("#task").load("select-task.php");
setInterval(function(){
    $.get("select-task.php", function (data) {
        if(data > 0)
        {
            $('#task').removeClass('not');
            $('#task').html(data);
        }
        else
            $('#task').addClass('not');
    });

}, 2000);
 });
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

please goto developer tools and trace the code, may be you are get result now, but have a problem in presentation, this is sample code and may be you need to change some presentation code
dear mohammad, we can trace your code in the jsfiddle, because we have a external php resource that name is: select-task.php, when we dont have any external.php in jsfiddle, so we can't trace problem...
here i put above the php and html and jquery code when i use your code $.get("select-task.php", function (data) { if(data > 0) { $('#span').removeClass('not'); $('#span').html(data); } else $('#span').addClass('not'); }); }, 2000); the class not not working don't give me the effect this effect should appear only when i have a notification number bigger than 0
mohammad, try to remove class "not" of you code, instead that, if data> 0 use: $('#task').addClass('badge-success') and $('#task').removeClass('badge-danger') and else try $('#task').addClass('badge-danger') and $('#task').removeClass('badge-success')
i solve it your code help but the if condition was wrong i've change to if(data==0)
0

You can use the callback function with load:

$("#task").load("select-task.php", someCallBackFunction);

setInterval(function() {
   $("#task").load("select-task.php", someCallBackFunction);
}, 20000);

function someCallBackFunction() {
  if($("#task").find("#span").length) {
      //do something...
  } else {
     // else do something...
  }
}

4 Comments

i do like this but it didn't work function someCallBackFunction() { if($("#task").find(".not").length) { $("#span").addClass("not"); } else { $("#span").removeClass("not"); } }
What is #span? Is that the ID of your element?
yes this is #span <span class="badge badge-success" id="span">
In that case, you should find("#span").. I have updated the code.

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.