Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Jquery notification event firing twice

I have just started using gritter notification plugin and experiencing strange behavior. I am displaying the notification on page load and for some reason, the same message is appearing twice.

I would be grateful if someone could check my code and highlight any errors that I have overlooked.

The link for the plugin is: http://boedesign.com/blog/2009/07/11/growl-for-jquery-gritter/.

Many thanks.

<div id="messages" style="float: left;height: 220px">
     <div>
       Messages
     </div>
     <div style="overflow: hidden;">
        <div id="message"style="font-size: 11px; padding: 10px 10px;" >

            <?php

                mysql_select_db($database_domain, $domain); 
                $query1 = "SELECT * FROM messages WHERE to_user = '$_SESSION[kt_name_usr]' AND `read` = 1"; 
                $result1 = mysql_query($query1) or die(mysql_error());
                $totalRows1 = mysql_num_rows($result1);
                if (mysql_num_rows($result1) ==0) {
                $error1 = true;
                $message3 = "You have no new messages.";
                } else {
                // There was a result
                // now check if there were any records.
                    if (mysql_num_rows($result1) >0) {    
                        $error1 = false;
                        $messages1 = "You have" . "  " . "<span class='yourstyle'>" . $totalRows1 . "</span>" . " " . "new message(s). Please go to the message area in your control panel."; 
                  }
                 } 

                 if($error1 == 0)  {

                    //echo "You have" . "  " . "<span class='yourstyle'>" . $totalRows1 . "</span>" . " " . "new message(s). Please go to the message area in your control panel.";

                    echo "<SCRIPT type=text/javascript>\n";
                    echo "$(function() {\n";
                    echo "$(\"#testid\" ).show()\n";
                    echo "$.gritter.add({title: 'This is a notice', text: \"".$messages1."\"})\n";
                    echo "});\n";
                    echo "</script>\n";

                } else { 

                    echo 'There are no new system or site messages to display.';

                }

             ?>

            </div>

           </div>

          </div>

          <div id="testid" style="display:none;"></div>

Open in new window

0
peter-cooper
Asked:
peter-cooper
  • 4
  • 3
  • 2
  • +2
1 Solution
 
leakim971PluritechnicianCommented:
could you post the generated HTML instead the PHP code?

I see you put a document.ready inside a loop, so it will fire more than one time
0
 
RobOwner (Aidellio)Commented:
Works fine here: http://jsbin.com/wemahi/1/edit

I would change a few things:

- You're missing semicolons at the end of your JS lines
- The SCRIPT tag should be lowercase.
- Remove this line just while you're testing:  echo "$(\"#testid\" ).show()\n";
- You have mixed use of quotes in your gritter call.  remove the double quotes and stick with the single: echo "$.gritter.add({title: 'This is a notice', text: '$messages1'});\n";

Given this is rendered on the client, can you post the rendered code please?  Load the page, right click, view source, paste here.
0
 
RobOwner (Aidellio)Commented:
@Leakim971 - where is the loop? I couldn't see it...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
peter-cooperAuthor Commented:
@Rob Lurd
Thank you for your comment.
You're missing semicolons at the end of your JS lines
Where exactly?

You have mixed use of quotes in your gritter call.  remove the double quotes and stick with the single: echo "$.gritter.add({title: 'This is a notice', text: '$messages1'});\n";
That doesn't work.

Renedered code as far as I can see.

<div id="messages" style="float: left;height: 220px">
                    <div>
                        Messages
                    </div>
                    <div style="overflow: hidden;">
			<div id="message" style="font-size: 11px; padding: 10px 10px;">
							
			<script type=text/javascript>
$.gritter.add({title: 'This is a new notice', text: "You have  <span class='yourstyle'>1</span> new message(s). Please go to the message area in your control panel.", time: 3000});
</script>
						
			</div>
                        
                    </div>
                </div>

Open in new window

0
 
Ray PaseurCommented:
I see two things that might make your life easier.  First, get off MySQL ASAP.  It's a dead technology.  This article explains why and what you must do to keep your scripts running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Second, when you see code like this:
echo "<SCRIPT type=text/javascript>\n";
echo "$(function() {\n";
echo "$(\"#testid\" ).show()\n";
echo "$.gritter.add({title: 'This is a notice', text: \"".$messages1."\"})\n";
echo "});\n";
echo "</script>\n";

Open in new window

get rid of it and instead use the HEREDOC notation.  It greatly simplifies the punctuation and escaping, and lets you concentrate on the contents of the data string (in this case, javaScript).
$thing = <<<EOD
<script type=text/javascript>
$(function(){
    $("#testid" ).show()
    $.gritter.add({title: 'This is a notice', text: "$messages1"});
});
</script>
EOD;
echo $thing;

Open in new window

I did not try to understand the JavaScript.  It looks a little "off" to me, and I'm not sure I understand what you want it to accomplish.
0
 
RobOwner (Aidellio)Commented:
Your rendered code is missing the jQuery initializing function. E.g. $(function() {})
The missing semicolons we're not in your php file at the end if every line of JavaScript (not php) as JavaScript requires then too. However your rendered code looks fine other than the missing jQuery init.
0
 
Ray PaseurCommented:
To Rob's point about jQuery initialization, this is how I start.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
});
</script>

Open in new window

Then building on that, I might come up with something like this.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    $("#testid" ).show()
    $.gritter.add({title: 'This is a notice', text: "$messages1"});
});
</script>

Open in new window

0
 
RobOwner (Aidellio)Commented:
And to build on what Ray's said, you should only have one initialisation function, not multiple.  The reason it's needed is the javascript needs to wait until all the HTML has been rendered by the browser.  There are a series of events that happen when you load a webpage and not all of them allow you to manipulate the html in the website.

The other accepted practice is to put the <script>...</script> just before the </body> tag.  I've never liked doing this but it's based on the principle that the content has been loaded by the time it gets to this point of the page.  It should otherwise be in the <head>

In terms of your page, I would personally output all the page as you want it to look and put the javascript all together in the <head> (or before the </body>) with all that needs to happen on your page.
0
 
peter-cooperAuthor Commented:
@Rob Lurd
Turned out to be quite simple. Took your advice and deleted original script code and declared new script before </body> tag and it works.

<script type="text/javascript">
       $(function () {
	   var my_php_var = <?php echo json_encode($messages1) ?>;
	   $.gritter.add({title: 'New Message', text: my_php_var, time: 4000, sticky: false});
	});
</script>

Open in new window


@Ray
Thank you for your comments. Will look into HEREDOC. Many thanks.
0
 
peter-cooperAuthor Commented:
Thank you very much for your help.
0
 
Michel PlungjanIT ExpertCommented:
@Rob

- You're missing semicolons at the end of your JS lines
- The SCRIPT tag should be lowercase.
Not mandatory as long as there are a newline after each line and it is not inline JS in an event handler.

Still good practice but not a source of the errors
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now