Is there anyway to load the newly inserted rows to the database without refreshing the page using jQuery? I am able to send the data to the database without refreshing using jquery but I am stuck at showing that data back without refreshing the page. How do I display the data from the table without refreshing page and make it appear under the table in index.php which I have to display the retrieved data? Thanks
This is my 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);
?>
And here is my send.php which submits the data to the table.
<?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";
?>