kawasaki Posted March 15, 2009 Share Posted March 15, 2009 How would i make a popup window appear when there is a new entry in a MySQL table? Like a popup window for NEW MAIL! Or something to that nature? Any ideas? Thanks. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 15, 2009 Share Posted March 15, 2009 You would have to send Ajax requests on time intervals (for example every minute). On the server have a script checking if there are new rows in table. Quote Link to comment Share on other sites More sharing options...
Malevolence Posted March 15, 2009 Share Posted March 15, 2009 Well it depends whether the user will be waiting for a message (without refreshing) or whether you mean when they log in, the popup will appear- and they'll have to click check 'mail' or whatever your purpose for it is, to see the new thing. Lets get the popup out of the way first. Goes in the <head> section of HTML <script type="text/javascript"> <!-- function popgotmail() { window.open("popgotmail.php","New Message!","status=1,height=200,width=300,resizable=0") } //--> </script> The PHP bit is simple, you will need to find the number rows that haven't been read in the db- if more than 0, then display the popup. Obviously, this means you're gonna need a field for 'read/unread' so go ahead and make it, call it something like 'opened'. Then, when the user opens a message, a php script will send a query to the database, appending the 1 to the opened field of that message. By the way this whole thing including what you plan in the future with it will need an ID field with an auto-increment set on that field and a primary key- not important for this script, but where you're going with it, you may need it <?php $host = "localhost"; $usr = "exampleuser"; $pwd = "examplepass"; $db = "databasename"; $tbl = "tablename"; $conn = mysql_connect($host,$usr,$pwd); if (!$conn) { die('Failed to Connect to DB Host:- ' . mysql_error()); } $dbconn = mysql_select_db($db,$conn); if (!dbconn) { die('Failed to Access DB:-' . mysql_error()); } $q1 = "SELECT * FROM $tbl WHERE opened = 'no'"; $r1 = mysql_query($q1,$conn); if(mysql_num_rows($r1) > 0) { $bodytag = "<body onLoad='popgotmail()'>"; } else { $bodytag = "<body>"; } mysql_close($conn); ?> Then just replace <body> in your page with <?=$bodytag?> or <?php echo $bodytag; ?> if short echo tags aren't enabled. Note that you will have to put in your own db details etc. Also, you'll need to make the page 'popgotmail.php' in the same directory as the main page that triggers teh popup. Unless you wanna tweak the code, which is fair enough Have fun, hope I didn't make a mistake there haha Oh and Mchl's post, yeah that bit will constantly check for new mail by running background page loads of a php file that simply opens the db, counts rows and either display popup, or not. I forgot to mention that myself. Quote Link to comment Share on other sites More sharing options...
kawasaki Posted March 30, 2009 Author Share Posted March 30, 2009 Thanks.. that should work. I will try it. What I am trying to accomplish is a instant messaging system. Any alternative way you would do this? So if a user was sitting on the website and there was a new entry in the mysql table. It needs to popup a new window with my im.php file containing the code to chat with the sender. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.