Jump to content

POPUP WINDOW on when a new entry in a MySQL TABLE


kawasaki

Recommended Posts

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.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.