2016-11-07 168 views
3

下午好! 我有一個與Php結合的HTML表。我想要做的是,當您向數據庫中輸入新數據時,表會刷新以顯示新數據,但只顯示錶格而不顯示頁面的其餘部分。老實說,我不是程序員,但我明白,它可以用JSON和Ajax完成,但不知道如何。如果有人有時間和耐心告訴我我多麼感激。只刷新一張HTML表格,而不是整個頁面

這是我的PHP代碼:

<?php 
// ----------- CONEXIÓN ------------------ 
    require 'RecibeConsultaPuenteConexion.php'; 
    //echo "Conexión OKAS <br />"; 
    // ----------- TRAYENDO DATOS ------------------  
    $statement = $conexion->query('SELECT * FROM tbl_consultas'); 
    $statement->execute(); 


$resultados = $statement->fetchAll(); 
foreach ($resultados as $fila) { 



} 





// json_encode($fila); 


?> 

,這是我的html代碼:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" > 
    <table class="Pizarra" id="pizarra" name="pizarra" cellspacing="0px";> 
     <tr class="trThDos"> 
     <th></th> 
     <th></th> 
     <th></th> 
     <th></th> 
     <th></th> 
     </tr> 
     <tbody> 
     <!-- Comienza PHP --> 
     <?php 
     $i = 0; 
     foreach ($resultados as $fila) { 
     ?> 
      <tr class="trPizarra" id="trPizarra"> 

      <td class="tdTurno" style="text-align:center;"><?php echo '#' . ++$i ?></td> 

      <td class="tdImg" style="text-align:center;"><?php echo $fila ['asunto']; 

        switch ($fila['asunto']){ 

        case "0": 
        echo "<img src='./img/consulta-56-2.png' title=\"Consulta.\" height=\"32\" width=\"32\">"; 
        break; 

        case "1": 
        echo "<img src='./img/shot-56-2.png' title=\"Inyección.\" height=\"32\" width=\"32\">"; 
        break; 

        case "2": 
        echo "<img src='./img/ta-56-2.png' title=\"Toma de presión.\" height=\"32\" width=\"32\">"; 
        break; 

        case "3": 
        echo "<img src='./img/cert-56-2.png' title=\"Certificado médico.\" height=\"32\" width=\"32\">"; 
        break; 

        case "4": 
        echo "<img src='./img/consulta-56-4.png' title=\"Consulta ⬇ abajo.\" height=\"32\" width=\"32\">"; 
        break; 

        case "5": 
        echo "<img src='./img/shot-56-4.png' title=\"Inyección ⬇ abajo.\" height=\"32\" width=\"32\">"; 
        break; 

        case "6": 
        echo "<img src='./img/ta-56-4.png' title=\"Toma de presión ⬇ abajo.\" height=\"32\" width=\"32\">"; 
        break; 

        case "7": 
        echo "<img src='./img/cert-56-4.png' title=\"Certificado médico ⬇ abajo.\" height=\"32\" width=\"32\">"; 
        break; 

        default: 
        echo "Hubo un error en la selección de asunto"; 
        break; 

        } ?></td> 

      <td class="tdNombre" id="tdNombre" style="text-align:center;"><?php echo $fila ['nombre_completo']; ?></td> 

      <td class="tdHr" style="text-align:center;"><?php echo $fila ['hora']; ?> <span class="icon icon-stopwatch"></span></td> 

      <td class="td-aceptar"> 
      <a class="aceptar-a" id="aceptar-a" href="http://localhost/FARMAXIA/index.php?id=<?php echo $fila['ID']; ?>" title="Aceptar paciente." > 
      <button class="btn btn-xs" id="aceptar-btn" ><span class="glyphicon glyphicon-ok"></span> Aceptar</button> 
      </a> 
      </td> 

      </tr> 
     <?php } ?> 
     <!-- Terminó PHP --> 
      <tfoot class="tableFoot"> 
       <tr> 
        <td colspan="5"><p>&nbsp;</p></td> 
       </tr> 
      </tfoot> 

     </tbody> 
    </table> 
    </form> 
+0

你用Ajax/JSON先試過了什麼?如果沒有,請查看它的快速教程,看看你是否可以自己實現它。 – Jhecht

+0

你爲什麼不嘗試使用jQuery數據表。它會自動照顧清爽和一切。 – Iftikhar

回答

2

你的基本裸露的骨頭代碼將是這個

jQuery代碼

//asuming you want to refresh your table every 5 sec 
function refresh_table() 
{ 
    $.ajax({ 
     type:"POST", 
     url:"my_refresh_php_code.php", 
     cache:false, 
     success:function(html){ // html returns the code outputted from the below php script 
      $("my_table_class_or_id").html(html); //replace your table html with the new one 
     } 
    }) 
} 

setInterval(refresh_table, 5000); 

您的「my_ refresh_php_code.php「腳本

<?php 

//output the data you want your table to be refreshed with 
echo "my text" 

?> 
+0

非常感謝您的幫助,它非常完美! – Emm

相關問題