我试着只用php、html和mysql进行一次小的聊天。它的主要问题是,当你写的时候,网页刷新了,你写的文本就消失了。我该怎么解决?我不能使用java,所以它更难。代码如下:
聊天(mysql连接详细信息已删除):
<?php
$_POST['text'] = '';
session_start();
include 'status.php';
$_SESSION['status'] = status($_SESSION['nick']);
if(IsSet($_SESSION['nick']) && IsSet($_SESSION['pass'])){
?>
<link rel="stylesheet" href="/style.css">
<link rel="icon" href="/icon.ico">
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "15";
header("Refresh: $sec; url=$page");
$_SESSION['text'] = $_POST['text'];
$con = mysqli_connect("","","","");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<body>
<h4>Chat: </h4>
<form align="center" name="form" method="POST" action="/chat/speak.php">
<?php echo $_SESSION['nick'] . ': '; ?>
<input name='text' type='text' value="<?php echo $_SESSION['text']; ?>" style="width: 500px;"><br>
Person: <select id="so" name="private">
<?php
$result = mysqli_query($con,"SELECT * FROM users ORDER BY nick");
while($row = mysqli_fetch_array($result))
{
$nick = $row['nick'];
echo "<option value='$nick' selected='selected'>$nick</option>";
}
?>
<option value="public" selected="selected"> PUBLIC </option>
</select>
<input type="submit" value="Speak!"></form>
</body>
<?php
echo '<hr/>';
$result = mysqli_query($con,"SELECT * FROM chat ORDER BY time DESC");
echo "<div align='left' name='chat'>
<h2 align='center'><u>Conversation: </u></h2>";
echo '<hr/>';
echo "<ul style='list-style-type:none'>";
while($row = mysqli_fetch_array($result))
{
$author = $row['author'];
$message = $row['message'];
$color = $row['color'];
$p = $row['p'];
$destinatary = $row['PRIVATE'];
if($p == 1){
if($author == $_SESSION['nick'] || $destinatary == $_SESSION['nick']){
echo "<li align='left'>" . "<b><font color='$color'>Private from: $author</font></b>" . "<b><font color='$color'> - </font></b>" . "<font color='$color'>$message</font><hr>";}
}else{
echo "<li align='left'>" . "<b><font color='$color'>$author</font></b>" . "<b><font color='$color'> - </font></b>" . "<font color='$color'>$message</font><hr>";
}}
mysqli_close($con);
echo '</ul>';
echo '</div>';
}else{
echo 'You are not logged!';
}
?>
主窗体的位置(mysql连接详细信息已被删除):
<?php
session_start();
$conn = new mysqli('', '', '', '');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(IsSet($_POST['text']) && ($_POST['text']) != ''){
if($_POST['private'] == 'public'){
$nick = $_SESSION['nick'];
$text = $_POST['text'];
$time = date('c');
$color = $_SESSION['color'];
$text = mysqli_real_escape_string($conn,$text);
$sql = "INSERT INTO chat (author, message, time, color, p, PRIVATE)
VALUES ('$nick', '$text', '$time', '$color', '0', '')";
if ($conn->query($sql) === TRUE) {
header('Location: /chat/chat.php');
} else {
echo "Error";
}
$conn->close();
}else{
$nick = $_SESSION['nick'];
$text = $_POST['text'];
$time = date('c');
$color = $_SESSION['color'];
$destinatary = $_POST['private'];
$text = mysqli_real_escape_string($conn,$text);
$sql = "INSERT INTO chat (author, message, time, color, p, PRIVATE)
VALUES ('$nick', '$text', '$time', '$color', '1', '$destinatary')";
if ($conn->query($sql) === TRUE) {
header('Location: /chat/chat.php');
} else {
echo "Error";
}
$conn->close();
}}else{
header('Location: /chat/chat.php');
}
?>
你能告诉我在哪里可以改进代码吗?提前感谢您的努力和花费的时间!
暂无答案!
目前还没有任何答案,快来回答吧!