1

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.

4

2 回答 2

0
I want it as soon as someone posted something, it updates the posts in the div tags with the id of messages.

您将不得不为该部分使用 javacript。其他一切都可以用 PHP 和 MySQL 完成。

前几天我看了很棒的 youtube 教程,了解如何做到这一点。它是一个“快速而肮脏”的解决方案,但应该可以为您提供一些基础。http://www.youtube.com/watch?v=FyXeOX-uYMc

于 2012-11-10T14:51:32.163 回答
0

首先创建一个页面 message.php 为:

        <?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>

然后创建一个页面 active.php 作为

<?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";
}
?>

这是很一般的聊天。现在你需要根据你的要求来改变它。

于 2012-11-10T15:22:20.897 回答