2013-08-26 125 views
0

我想從一個或多個div中的默認內容的MySql數據庫中交換數據,而無需重新加載頁面。我已經在Dreamweaver CS6中使用'set text of container'javascript行爲插入PHP代碼,但沒有成功。Javascript,AJAX將MySQL內容轉換爲div

如果有人能告訴我如何做到這一點,我將不勝感激。

+0

嘗試[** **阿賈克斯(https://www.google.com/search?q=ajax)。 –

回答

0
save your php code in a file named getinfo.php then call it with ajax 

<?php 
    include("connectdatabase.php"); 

    $sql="SELECT * FROM table ORDER BY id DESC"; 
    $result=mysqli_query($con,$sql); 

    $i = 1; 
    while ($row = $result->fetch_assoc()) { 
    echo "<tr>"; 
    echo "<td>" . $i . "</td>"; 
    echo "<td>" . $row["Name"] . "</td>"; 
    echo "<td>" . $row["Score"] . "</td>"; 
    echo "</tr>"; 
    $i++; 
    } 
    mysqli_close($con); 
?> 

在你的主文件,請使用此

<div class="scoreBoard"></div> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
    setInterval(refreshScoreBoard, 5000); 
    var inRequest = false; 
    function refreshScoreBoard() { 
    if (inRequest) { 
     return false; 
    } 
    inRequest = true; 
    var load = $.get('getinfo.php'); 
    $(".scoreBoard").html('Refreshing'); 
    load.error(function() { 
     console.log("Error"); 
     // do something here if request failed 
    }); 
    load.success(function(res) { 
     console.log("Success"); 
     $(".scoreBoard").html('<table>'+res+'</table>'); 
    }); 
    load.done(function() { 
     console.log("Completed"); 
     inRequest = false; 
    }); 
    } 
</script>