SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Alert message

  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alert message

    Hello,

    I wanted to build-up a module where if any user gets any shout/comment then send alert message to that user who has got this shout/comment.so when that users loggs in, he/she ll get this alert message.so he/she can see that message(like here on SP,if we get any PM, we gets alert message once we have logged in)

    I have done this through mail. but i just want to build up something like this.

    Can anyone tell me how can i do this?

    Thnaks in advance.

  2. #2
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Not too complicated. In PHP, you check if the user has any new comments. If they do, then output a JavaScript alert like:

    Code:
    alert('You have a new comment from <?php echo addslashes($username) ?>.');
    If you want it to happen in realtime without the user having to refresh the page, that's a different story. You'd have to look into AJAX for that.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply devbanana,

    See, this is somthing i have done with email notification, if message is inserted into database. send message to that user.

    PHP Code:
     if($validate == 1){
           
    $messagesql              $op->insert("INSERT INTO ".tbl_message." (toid,fromid,message,date) VALUES ('".$to."','".$from."','".$message."','".date('Y-m-d:h:i')."')");

                    if(
    $messagesql 0){
     echo 
    "SELECT a.username, a.fname as  authorname, a.email, a.auth FROM ".tbl_author." a WHERE  a.id='$to'";

                     
    $sql $op->runsql("SELECT a.username, a.fname as  authorname, a.email, a.auth FROM ".tbl_author." a WHERE  a.id='$to'");
                              if(!
    $sql){ return false; }

                          
    $row $op->select($sql);


                              
    $email             =   $row['email'];
                              
    $fromemail         =  abc@asd.in';
                            $mailbody         .= "<p>Regards.</p>";
                                 $mailbody         .= "<b>Author'
    s email:</b".$email."<br>";
                            
    $subject           = "YOu have got new Message";
                                 
    $fromemail         = $fromemail;
                                 
    $to                = $email;
                                 
    $msg               = $mailbody;
                            
    $headers           = "MIME-Version1.0" . "\r\n";
                                 
    $headers          .= "Content-typetext/htmlcharset=iso-8859-1" . "\r\n";
                                 
    $headers      .= "From:".$fromemail. "\r\n";
                                 
    $headers;
                            if(mail(
    $to$subject$msg$headers)){
                              echo "
    Mail has been sent Successfully.";
                           } else {
                                                        echo "
    Mail has not been sent Successfully.";}


                          }else{
                          
    $err_msg_shout="Error, Try Again";
                   } 
    may be its easy, but how can i do this with JS alert i dont know. could you please elaborate what your trying to suggest me.

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are doing that when the comment is sent, yes. But the alert has to be on the target user's end.

    For instance, if Bob sends a message to Pete. When Pete next logs in, you have to check if he has any new comments, and output the alert as I posted.

    I don't know if you have a field in the database specifying whether a message is read or not, but that's what you'll want to use. Select the messages from the database for the user viewing the page, where `read` = false (or 0). Then pop up a message to the user telling them they got a new comment and from whom it was sent.

    You'll want to make sure it does not do this on the actual comments page so that there's not an endless loop of alerts.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  5. #5
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you have a field in the database specifying whether a message is read or not, but that's what you'll want to use. Select the messages from the database for the user viewing the page, where `read` = false (or 0).
    how can we do that? how can we know that the message is read by the user or not. may be with last_access time like when the post is posted check that time to the last_access.

    is that what you are suggesting?

  6. #6
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any Help??

  7. #7
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deepson2 View Post
    how can we do that? how can we know that the message is read by the user or not. may be with last_access time like when the post is posted check that time to the last_access.

    is that what you are suggesting?
    Pretty simply. When the user views the comment, update the comment, setting `read` = 1.

    For instance:

    Code:
    UPDATE `comments` SET `read` = 1 WHERE `id` = 1
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i understand the logic but i am not getting how to write JS for it and where to call it like when the comment is posted or after that?

    I have done only validations with JS. could you please show me only the flow, how can i do that?

  9. #9
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"><head>
    <form name="messageFrm" action="<?$PHP_SELF;?>" method="post">

    <script type="text/javascript">
    var $check1 = <?=$check?>;
    if($check1 == 'Y')
    {
    alert ("You have an unread message");
    }
    else
    {
    document.write ("");
    }
    </script>
    </head>
    <?if(!$userid){?>
    <body id="free">
    <?}else{
     
    ?>
    <body>
    <?}?>
    <?
    //this record is exist in database
    echo "SELECT * FROM comments where userid='$userid' and blogid='11072'";
    $result mysql_query("SELECT *  FROM comments where userid='88' and blogid='11072'");
             if(
    mysql_num_rows($result) > 0){

    while(
    $row mysql_fetch_array($result))
    {
    $check$row['chkstatus'];
    }}
    ?>
    </form>
    </body>
    </html>
    I have tried this. just to check whether i am getting alert message or not. no luck.

    any one can tell me how can i get alert message here?

  10. #10
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are you confused about what I've told you in my previous posts?

    By the way, your HTML is very much invalid. The <form> tag never goes before <body>.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  11. #11
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deepson2 View Post
    ok, i understand the logic but i am not getting how to write JS for it and where to call it like when the comment is posted or after that?

    I have done only validations with JS. could you please show me only the flow, how can i do that?
    Essentially, when user loads page (pseudocode):

    Code:
    if (has_unread_comments($username))
    {
    ?>
    <script type="text/javascript">
    alert('You have unread comments.');
    </script>
    <?php
    }
    Now obviously there's more to it. has_unread_comments() is just a placeholder for actually checking if the user has unread comments. But essentially it's a simple if statement, nothing more than that.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  12. #12
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a cleaner way to do this using a mix of css to do this? I find those javascript popups quite annoying.

  13. #13
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have two options. Using a javascript alert box is not the right way to go, you are correct. You need to give the user the option of viewing the comment or continuing to the forum. You could do this with a JavaScript confirm box, which gives you OK/Cancel buttons without any extra work. Another option is redirect them to a page that says "you have a new message," with view/continue to forums. The third option is a "modal" dialog box. JavaScript frameworks like mootools, jquery, have plugins that make modal creation feasible. It's basically a div that pops up over the page, feels less intrusive than a javascript alert box. You've probably seen it before on various websites.

    PS I JUST SENT YOU A PM so that you can see how this forum handles messages.

  14. #14
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do this with a JavaScript confirm box, which gives you OK/Cancel buttons without any extra work. Another option is redirect them to a page that says "you have a new message," with view/continue to forums. The third option is a "modal" dialog box. JavaScript frameworks like mootools, jquery, have plugins that make modal creation feasible. It's basically a div that pops up over the page, feels less intrusive than a javascript alert box. You've probably seen it before on various websites.
    Thanks for your reply but firstly i want to get that alert message work, then ll have to think on where to redirect my page and other things.

  15. #15
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana View Post
    Essentially, when user loads page (pseudocode):

    Code:
    <?
    while($row = mysql_fetch_array($result))
    {
    $check= $row['chkstatus'];
    if ($row['chkstatus']=='Y')
    {
    ?>
    <script type="text/javascript">
    alert('You have unread comments.');
    </script>
    <?php
    }
    }}
    ?>
    Now obviously there's more to it. has_unread_comments() is just a placeholder for actually checking if the user has unread comments. But essentially it's a simple if statement, nothing more than that.
    thanks devbanana, now i am getting alert message. and now as you said i have to see where i can update my read=1 status? then where i can redirect my page? right?

  16. #16
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For future consideration, I like to put a small fixed square in a corner of the window. It catches your attention, but it's not annoying, so you can ignore it until later.

  17. #17
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On index.php

    PHP Code:
    <?php

    echo "SELECT c.comment, c.blogid, c.chkstatus
    FROM comments AS c, bloginfo AS b
    WHERE c.blogid = b.id
    AND b.author = '
    $userid'
    AND c.chkstatus = 'N'"
    ;
    $result mysql_query("SELECT c.comment, c.blogid, c.chkstatus
    FROM comments AS c, bloginfo AS b
    WHERE c.blogid = b.id
    AND b.author = '
    $userid'
    AND c.chkstatus = 'N'"
    );
             if(
    mysql_num_rows($result) > 0){

    while(
    $row mysql_fetch_array($result))
    {
    $check$row['chkstatus'];
    if (
    $row['chkstatus']=='N')
    {
    ?>
    <script type="text/javascript">
    alert('You have unread comments.');
    </script>
    <?php
    }
    }}
    ?>
    and then on blogprofile.php
    PHP Code:
    <?
    echo "Update ".tbl_comments." Set chkstatus = 'Y' where blogid='$blogid'";
    $sSqlSec =  $op->runsql("Update ".tbl_comments." Set chkstatus = 'Y' where blogid='$blogdata->blogid'");
    ?>
    With these query i am able to show alerts and then once user checks his/her blog's comment then status changes from 'N' to 'Y'.

    Now the issue is redirection. when i logged in if i have comment on my blog then firstly my index page goes blank with only alert message. and when i click on ok then it goes to its index page.

    how can i get index page as well as this alert message at time?

    please help me.
    Last edited by deepM; Jun 30, 2009 at 21:27.

  18. #18
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, im new to this forum. i just cant find a way to create a new thread/topic i have just registered.

    please can someone help as the FAQ is really useless for that.
    thanks.

  19. #19
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, im new to this forum. i just cant find a way to create a new thread/topic.
    I have just registered.

    please can someone help as the FAQ is really useless for that.
    thanks.

  20. #20
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried the 'New Thread' button? Seems like an obvious place to start...

  21. #21
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello.
    i fell a bit stupid, i found that button now.
    i was expecting this tool in "mySitepoint" section" lol

    i must say i rarely use web forums but i remember how it works now .

    thanks.

  22. #22
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and i also mixed up the general forum subjects and the threads wich of course are 1 step deeper in the hierarchy.
    sorry for that.

  23. #23
    I &lt;3 Internet Tekime's Avatar
    Join Date
    Dec 2003
    Location
    Maine
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use a status field in the DB for each message, such as an UNSIGNED TINYINT(1) field with numbers corresponding to message status:

    0 - Unread, notification has not been sent
    1 - Unread, notification has been sent
    2 - Read - message has been viewed/marked as read
    3 - Trash - message is in trash + hidden from inbox

    I don't know what your exact status numbers will need to be, it sounds like you need to work up a small spec and define exactly what you want.

    Another little tip: if you're sending email alerts of any kind, from a cronjob or automatic script, update the message status before you send the email, then update the message, to avoid sending multiple copies inadvertently.
    Scriptalicious SEO Scripts
    Save 20% with coupon code SPROCKS


  24. #24
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tekime,

    I am done with this what you have suggested here.but the remaining issue is:

    Now the issue is redirection. when i logged in if i have comment on my blog then firstly my index page goes blank with only alert message. and when i click on ok then it goes to its index page.
    so can anyone tell me,how can i get index page as well as this alert message at time?

  25. #25
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jQuery UI has a Dialog box that might work well for what you need.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •