有沒有辦法將新插入的行加載到數據庫,而無需使用jQuery刷新頁面?我能夠將數據發送到數據庫而無需使用jquery進行刷新,但我堅持在不刷新頁面的情況下顯示該數據。如何在不刷新頁面的情況下顯示錶格中的數據,並使其顯示在index.php中的表格下,我必須顯示檢索到的數據?由於如何從數據庫顯示數據而無需使用jQuery刷新頁面,PHP
這是我的index.php
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
type: "POST",
url: 'send.php',
data: "user=" + $('#user').val() + "&comment=" + $('#comment').val(),
success: function(data){
$('input[type=text]').val('')
$('#status').html(data);
}
});
});
});
</script>
</head>
<body>
<div>
<input type="text" name="user" id="user">
<input type="text" name="comment" id="comment">
<input name="submit" type="submit" id="submit">
<div id="status"></diV>
</div>
<br>
<?php
$con=mysqli_connect("localhost","root","","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM comments");
echo "<table width='640' >
<tr>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr >";
echo "<td style='vertical-align:text-top'>" . $row['user'] . "</td>";
echo "<td><br><br><br>" . wordwrap($row['comment'], 90, '<br>',true) . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
這裏是我的send.php將數據提交給表。
<?php
$mysqli = new mysqli("localhost", "root", "", "user");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$username = $_POST["user"];
$comment = nl2br($_POST['comment']);
$stmt = $mysqli->prepare("INSERT INTO comments (user, comment) VALUES (?,?)");
$stmt->bind_param('ss', $username, $comment);
$stmt->execute();
echo"comment posted successfully";
?>
jQuery [load()](http://api.jquery.com/load/) –
檢查「related」邊欄 - 有許多ajax示例。您最好的選擇是瞭解他們如何工作,以便您可以將相同的原則應用於您的項目。 – showdev