Search code examples
phphtmlposts

Creating a Auto Updating <div></div> When Information Was Pasted on Mysql


It's my first day here, I thought you guys could help me. So, I'm working on this kind of chatting service for a friend. I want it to auto update anything inside the div tags that has an id of messages. I tried adding some javascript and stuff from other posts, but they just made it worse. So, if anyone could fix it and comment, or give me suggestions. I want it as soon as someone posted something, it updates the posts in the div tags with the id of messages.

<?php

session_start();

if(!isset($_SESSION['username']))
{

header("Location: /index.php");

}

?>
<html>

<head>

<title>
House Email System
</title>

<style>

body
{
background: #383838;
font-family: Arial;
color: #444444;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -150px;
}

form
{
background: #ffffff;
width: 270px;
border-radius: 15px;
box-shadow: 3px 3px 6px 1px #303030;
}

input
{
margin-left: 15px;
}

p
{
margin-left: 15px;
}

error
{
padding:2px 4px;
margin:0px;
border:solid 1px #FBD3C6;
background:#FDE4E1;
color:#CB4721;
font-size:14px;
font-weight:bold;
}

div
{
background: #f0f0f0;
}

hr
{
color: #d0d0d0;
background-color: #d0d0d0;
height: 3px;
border: 0px;
}

username
{
margin-left: 5px;
}

message
{
margin-left: 10px;
}

</style>

</head>

<body>

<?php

$connect = mysql_connect("localhost", "root", "*******");
mysql_select_db("*******");

$username = $_SESSION['username'];
$message = $_POST['message'];
$submit = $_POST['submit'];

if($submit)
{

if($message)
{

mysql_query("INSERT INTO messages VALUES('', '$username', '$message')");

}
else
{

?>

<center>

<error>Please enter a message to send.</error> <br />

</center>

<br />

<?php

}

}

?>

<form action="active.php" method="POST">

<br />

<p>Message</p>
<input type="text" name="message" /> <br />

<br />

<input type="submit" name="submit" value="Submit" /> <br />

<br />

<?php

$query = mysql_query("SELECT * FROM messages ORDER BY id DESC");
$num_rows = mysql_num_rows($query);

if($num_rows > 0)
{

while($row = mysql_fetch_assoc($query))
{

$username = $row['username'];
$message = $row['message'];

?>

<div id="messages">

<br /> <username><b><?php echo $username; ?></b></username>

<hr />

<message><?php echo $message; ?></message> <br />

<br /> </div> <br />

<?php

}

}
else
{

?>

<center>

<error>There are no messages to display.</error> <br />

</center>

<br />

<?php

}

?>

</form>

</body>

</html>

I hope you guys can help me, thank you.


Solution

  • First create a page message.php as:

            <?php
    session_start();
    ?>
    <html>
    
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"     type="text/javascript"></script>
    
    <script type="text/javascript">
            $(document).ready(function()
            {
                $('#submit').click(function()
                {
                //alert("hello");
               // ss=document.getElementById("message").value;
    
                    $.ajax({
                          type : 'POST',
                          url : 'active.php',
    
                         data: {
                         messages :$('#message').val()
                           },
                          success : function(data){
    
                             $('#messages').html(data);
    }
    
                    });
    
    
                });
            });
        </script>
    
    <title>
    House Email System
    </title>
    
    <style>
    
    body
    {
    background: #383838;
    font-family: Arial;
    color: #444444;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -75px;
    margin-left: -150px;
    }
    
    form
    {
    background: #ffffff;
    width: 270px;
    border-radius: 15px;
    box-shadow: 3px 3px 6px 1px #303030;
    }
    
    input
    {
    margin-left: 15px;
    }
    
    p
    {
    margin-left: 15px;
    }
    
    error
    {
    padding:2px 4px;
    margin:0px;
    border:solid 1px #FBD3C6;
    background:#FDE4E1;
    color:#CB4721;
    font-size:14px;
    font-weight:bold;
    }
    
    div
    {
    background: #f0f0f0;
    }
    
    hr
    {
    color: #d0d0d0;
    background-color: #d0d0d0;
    height: 3px;
    border: 0px;
    }
    
    username
    {
    margin-left: 5px;
    }
    
    message
    {
    margin-left: 10px;
    }
    
    </style>
    
    </head>
    
    <body>
    
    
    
    <center>
    
    <error>Please enter a message to send.</error> <br />
    
    </center>
    
    <br />
    
    
    
    <form action="active.php" method="POST">
    
    <br />
    
    <p>Message</p>
    <input type="text" name="message" id="message" /> <br />
    
    <br />
    
    <input type="button" id="submit" name="submit" value="Submit" /> <br />
    </form>
    <br />
    
    <div id="messages">
    
    
    
    </div>
    
    </body>
    
    </html>
    
    
    
    <script language="JavaScript"><!--
    // v3 compatible:
    function doSomething() {
       // do something here...
       $.ajax({
                          type : 'POST',
                          url : 'active.php',
    
                         data: {
                         messages :$('#message').val()
                           },
                          success : function(data){
    
                             $('#messages').html(data);
    }
    
                    });
       setTimeout('doSomething()',3000);
    }
    setInterval('doSomething()',3000);
    //--></script>
    

    Then Create a page active.php as

    <?php
    session_start();
    $connect = mysql_connect("localhost", "root", "");
    mysql_select_db("test");
    
    $username=$_SESSION['username'];
    $message=$_POST['messages'];
    
    if($message!="")
    {
    mysql_query("INSERT INTO messages VALUES('', '$username', '$message')");
    }
    
    $query = mysql_query("SELECT * FROM messages ORDER BY id DESC limit 1,0");
    $num_rows = mysql_num_rows($query);
    
    if($num_rows > 0)
    {
    
    while($row = mysql_fetch_assoc($query))
    {
    
    $username = $row['username'];
    $message = $row['message'];
    
    ?>
    
    <div>
    
    <br /> <username><b><?php echo $username; ?></b></username>
    
    <hr />
    
    <message><?php echo $message; ?></message> <br />
    
    <br /> </div>
    
    
    <br />
    
    <?php
    
    }
    
    }
    else
    {
    
    echo"No message to show";
    }
    ?>
    

    This is very general chat.Now you need to change it according to your requirements.