2016-10-21 3 views
-2

私は弟のためのウェブサイトを作っていますが、1ページを除いてすべてうまくいっています...私の兄がすべてを見ることができるこのページがあります他のページで視聴者が送信したメッセージだから私は彼のために、メッセージデータベースのテーブルのすべての要素を表示し、そのメッセージの「見た」状態に従って色を変える右へのリンク(色付きのボックスとして表示)を持っています。クリックされるとウェブページをリロードする2回 - PHP/Jquery

Descriptive image of what the web page looks like and what is in it

は、ボックスが1から0に見られる状態を変更、またはその逆のSQLクエリを作成してください。しかし、クリックするとページが2回リロードされるように見えます。そのため、クリックしたときの見た目の状態を維持します(一度リロード=見た目の状態が変更され、2回リロード=見た目が元に戻ります)。さらに、このロードロードの理論では、ページがロードされたあとに新しいメッセージをデータベーステーブルに挿入し、ボックスの1つをクリックすると、2つの新しいメッセージが残っていました。

このリンク(ボックスで表示)をクリックすると、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(); 
     } 
    } 

?> 

このページが毎回2回リロードされている理由を知ることができれば、最終的にウェブサイトを公開することができます。また、これは私の非常に初期のPHP段階であるので、「悪い」コーディングをお許しください。 - >アドバイスも歓迎します。

お礼、 ネイティブ。

答えて

1

編集:コメントに示唆されているとおり、これは正解ではありません。 dedi_table.phpは、最初のページのロード時にPHPをロードされている場合PlainObjectまたは文字列または配列

:jQueryのドキュメントには、それが配列としてだけでなく、文字列

データタイプを受け入れることを教えてくれるeditidが設定されているため、あなたのJavaScriptを挿入しています。そのファイルがロードされると、editid get paramaterが設定されていないため、2つのリロードを受け取ります。 <script>タグをDOMに配置し、最初のページの読み込み時に再読み込みできるはずです。

関連する問題