我已經爲我的兄弟做了一個網站,除了一頁之外它都運行良好...我的兄弟應該能夠查看所有頁面觀衆在另一頁上發送的消息。所以我有這個頁面給他看,它顯示了消息數據庫表中的所有元素,並帶有一個右側的鏈接(顯示爲彩色框),根據該消息的「已看到」狀態改變顏色。網頁被重新加載兩次 - PHP/Jquery
Descriptive image of what the web page looks like and what is in it
在被點擊時,框應使一個SQL查詢,其改變從1可見狀態爲0,或反之亦然。但似乎點擊後,頁面重新加載兩次...因此保持看到的狀態,因爲它是當我們點擊(重新加載一次=看到的狀態改變,重新加載兩次=看到的狀態恢復到初始狀態)。另外,我測試了這種雙重加載的理論,它通過在加載頁面的時間內向數據庫表中插入一條新的消息加載,並且一旦點擊其中一個框,就會有兩條新消息出現超時。
當點擊該鏈接(顯示爲一個框)時,url具有?editid = x值,我用它來修改數據 - > $ _GET ['editid']。
這裏是一個數據庫返回表元素的代碼:
<center>
<?php
session_start();
require("dbconnect.php");
if(isset($_SESSION['name'])){
global $connect;
echo("
<center>
<section>
</section>
<table id='dedi-table'>
<tr>
<th>Pseudo</th><th>Dédicace</th><th>Date</th><th>ID</th><th>Actions</th>
</tr>
");
$tablequery = "select * from dedi order by Temp desc";
$query = mysqli_query($connect, $tablequery);
if ($query){
while($row=mysqli_fetch_assoc($query)){
$pseudo = $row['Pseudo'];
$message = $row['Message'];
$temp = $row['Temp'];
$id = $row['ID'];
$seen = $row['seen'];
if ($seen == 1){
$color = 'green';
}else{
$color = 'red';
}
echo("
<tr>
<td>$pseudo</td><td>$message</td><td>$temp</td><td>$id</td><td><a class='view-link' href='dedi.php?editid=$id'><div class='seen-box' style='background-color: $color'> </div></a></td>
</tr>
");
}
echo("
</table>
</center>
");
}else{
echo("<div class='error-msg'>Problem With Database Table</div>");
}
}
?>
</center>
這是與初始頁面,並檢查登錄的代碼,以及包括上面的代碼(其是在名爲dedi_table文件.php)使用jQuery(所以它經常更新)。
<?php
session_start();
require("dbconnect.php");
global $connect;
if($connect){ // If able to connect to DB
if(isset($_POST['logoutbtn'])){
session_destroy(); //Disconnect
show_log_in();
}else{
if(isset($_POST['login'])){ //if the login button was pressed
check_log_in();
}else if(!isset($_SESSION['name'])){
show_log_in();
}else if(isset($_SESSION['name'])){//if a session has already been made
show_page();
}else{
echo("...Something went wrong...");
}
}
}else{ //If not able to connect to DB (connection failed)
//Show error
echo("Error - Please warn your website developper");
}
function check_log_in(){
global $connect;
$usr = htmlspecialchars($_POST['username']); //Recuperate the username input
$pass = htmlspecialchars($_POST['password']); //Recuperate the password input
$request = "select * from users where name = '$usr' and password = '$pass' ";
$query = mysqli_query($connect,$request); //SQL request
if ($query && $row=mysqli_fetch_assoc($query)){
$_SESSION['name'] = $row['name'];
$_SESSION['time'] = time();
show_page();//show the dédicaces
}else{
echo("<center><div class='error-msg'>Wrong username or password...</div></center>");
show_log_in();
}
}
function show_log_in(){ //Show login window
echo("
<center>
<div style='width: 100%; height: 10px; background-color: #5370C6;'></div>
<section class='inline-block skew' id='message-box'>
<div class='unskew'>
<center id='login-title'>Black Onyx Radio<br><b style='color: #CED8F6'>Login</b><hr id='underline-title'></center>
<form method='post' style='text-align: left;'>
<input type='text' name='username' placeholder='Username' class='input-text sm skew jscheck' style='margin-left: 10px;'><br>
<input type='password' name='password' placeholder='Password' class='input-text lg skew jscheck'>
<input type='submit' name='login' value='Login' class='btn-lg no-back no-border' id='send-message'>
</form>
</div>
</section>
</center>
");
}
function show_page(){ //Show the page with dédicaces
global $connect;
if (isset($_GET['editid'])){ //If the user has clicked on a 'vu' button (or colored box)
$edit_id = $_GET['editid'];
$sql = "select seen from dedi where ID = $edit_id";
$query = mysqli_query($connect, $sql);
if (mysqli_fetch_assoc($query)['seen'] == 0){
$sql = "update `dedi` set `seen` = 1 where `ID` = '$edit_id'";
$query = mysqli_query($connect, $sql1);
}else{
$sql = "update `dedi` set `seen` = 0 where `ID` = '$edit_id'";
$query = mysqli_query($connect, $sql2);
}
//Below is a jQuery script which reloads the table once, making sure we don't have to reload the page each time to see the changes after updating the sql database.
echo("
<script>
function updateTable(){
$('#table-container').load('dedi_table.php');
console.log('Updating Table Due To Click');
clearTimeout();
}
setTimeout(updateTable,200);
</script>
");
}
if (time() - $_SESSION['time'] <= 60*120){
$username = $_SESSION['name'];
$sess_time = date('h:i',$_SESSION['time']);
echo("
<section id='info-top-bar'>
<div class='inline-block' id='left-info'>Logged in as $username at $sess_time</div>
<div class='inline-block' id='right-info'><form method='post'><input type='submit' value='Log Out' name='logoutbtn' class='btn-black'></form></div>
<div style='width: 100%; height: 10px; padding: 0px; background-color: #5370C6;'> </div>
</section>"
); //Admin Menu goes here
echo("<script src='dedi.js''></script>"); //Javscript code which updates the table frequently
echo("<div id='table-container' style='width: 100%;'></div>");
}else{
echo("<div id='error-msg'>Session Timed Out - <a href='dedi.php'>Reconnect</a></div>");
session_destroy();
}
}
?>
如果你能弄清楚爲什麼每一次在地球上這個頁面重新加載的兩倍,這將是偉大的,因爲我終於可以發佈您的網站。此外,請原諒任何'壞'編碼,因爲這是我很早的PHP階段 - >任何意見也歡迎。
此致敬禮, 原生。