网络刷新时,HTML表单中的数据消失了

时间:2018-07-04 12:45:05

标签: php html mysql chat

我尝试仅使用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');
}
?>

您还能告诉我在哪里可以改进代码?在此先感谢您的努力和所花的时间!

0 个答案:

没有答案