เวลากดรีเฟรชแล้วมันขึ้นข้อมมูลซ้ำแก้ยังไงดีครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา เวลากดรีเฟรชแล้วมันขึ้นข้อมมูลซ้ำแก้ยังไงดีครับ

เวลากดรีเฟรชแล้วมันขึ้นข้อมมูลซ้ำแก้ยังไงดีครับ
1
2
3
4
5
6
CREATE TABLE IF NOT EXISTS `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` text NOT NULL,
  `sender` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
index.php
 
 
 
<?php
session_start();
 
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("chat", $con);
        $message=$_POST['message'];
        $sender=$_POST['sender'];
        mysql_query("INSERT INTO message(message, sender)VALUES('$message', '$sender')");
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Chat</title>
<script language="javascript" src="jquery-1.2.6.min.js"></script>
<script language="javascript" src="jquery.timers-1.0.0.js"></script>
<script type="text/javascript">
 
$(document).ready(function(){
   var j = jQuery.noConflict();
    j(document).ready(function()
    {
        j(".refresh").everyTime(1000,function(i){
            j.ajax({
              url: "refresh.php",
              cache: false,
              success: function(html){
                j(".refresh").html(html);
              }
            })
        })
         
    });
    j(document).ready(function() {
            j('#post_button').click(function() {
                $text = $('#post_text').val();
                j.ajax({
                    type: "POST",
                    cache: false,
                    url: "save.php",
                    data: "text="+$text,
                    success: function(data) {
                        alert('data has been stored to database');
                    }
                });
            });
        });
   j('.refresh').css({color:"green"});
});
</script>
<style type="text/css">
.refresh {
    border: 1px solid #3366FF;
    border-left: 4px solid #3366FF;
    color: green;
    font-family: tahoma;
    font-size: 12px;
    height: 225px;
    overflow: auto;
    width: 400px;
    padding:10px;
    background-color:#FFFFFF;
}
#post_button{
    border: 1px solid #3366FF;
    background-color:#3366FF;
    width: 100px;
    color:#FFFFFF;
    font-weight: bold;
    margin-left: -105px; padding-top: 4px; padding-bottom: 4px;
    cursor:pointer;
}
#textb{
    border: 1px solid #3366FF;
    border-left: 4px solid #3366FF;
    width: 320px;
    margin-top: 10px; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; width: 415px;
}
#texta{
    border: 1px solid #3366FF;
    border-left: 4px solid #3366FF;
    width: 410px;
    margin-bottom: 10px;
    padding:5px;
}
p{
border-top: 1px solid #EEEEEE;
margin-top: 0px; margin-bottom: 5px; padding-top: 5px;
}
span{
    font-weight: bold;
    color: #3B5998;
}
</style>
</head>
<body>
<form method="POST" name="" action="">
<input name="sender" type="text" id="texta" value="<?php echo $sender ?>"/>
<div class="refresh">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("chat", $con);
 
$result = mysql_query("SELECT * FROM message ORDER BY id DESC");
 
 
while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';
  }
 
mysql_close($con);
?>
 
</div>
<input name="message" type="text" id="textb"/>
<input name="submit" type="submit" value="Chat" id="post_button" />
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
refresh.php
 
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("chat", $con);
 
$result = mysql_query("SELECT * FROM message ORDER BY id DESC");
 
 
while($row = mysql_fetch_array($result))
  {
  echo '<p>'.'<span>'.$row['sender'].'</span>'. '&nbsp;&nbsp;' . $row['message'].'</p>';
  }
 
mysql_close($con);
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
save.php
 
<?php
 
if (isset($_POST['text'])) {
 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("chat", $con);
 
$ddd=$_POST['text'];
    $query = "INSERT INTO message (message) VALUES ('$ddd')";
     
}
 
?>


Abc999 20-02-2017 15:01:23

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
  เวลาเพิ่มข้อมูลเสร็จแล้ว ให้รีโหลดหน้านั้นๆ ใหม่เพื่อล้างการส่งค่าที่ POST 

ประมาณนี้ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("chat", $con);
        $message=$_POST['message'];
        $sender=$_POST['sender'];
        mysql_query("INSERT INTO message(message, sender)VALUES('$message', '$sender')");
        header("Location:index.php");
        exit;
}


แต่ถ้าใช้คำสั่ง header() แล้ว error 
ลองเปลี่ยนเป็นประมาณนี้แทนได้

1
2
echo '<meta http-equiv="refresh" content="0;URL=index.php" />';
exit;          




ninenik 20-02-2017
 ความคิดเห็นที่ 2
ได้แล้วครับผมขอบคุณมากครับ


abc999 20-02-2017 15:30
 ความคิดเห็นที่ 3


โทดนะครับ ถ้าผมอยากส่งข้อมูลแบบไม่ให้มันรีเฟรชหน้าใหม่ นี่ผมต้องทำไง #มือใหม่หัดเขียน ช่วยแนะนำด้วยครับ


abc999 20-02-2017 21:58
 ความคิดเห็นที่ 4
  ลองค้นหรือศึกษาข้อมูลเกี่ยวกับการใช้งาน ajax


ninenik 20-02-2017
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ