I have small piece of code for this. might be you accept!
JQuery:
<script>
$(document).ready(function() {
view();
});
function register()
{
var f = $('#fname').val();
var l = $('#lname').val();
$.ajax({
url:'index.php/control/insert',
data :{fname : f, lname : l},
type:'post',
success:function(msg){
return view();
}
});
}
function view()
{
$.ajax({
url:'index.php/control/show',
data :{},
type:'post',
success:function(msg){
$('#table').html(msg);
}
});
}
</script>
HTML Form:
<form action="" method="post">
<table align="center" border="11">
<tr>
<td>First name</td>
<td><input type="text" name="fname" id="fname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" id="lname" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" id="submit" name="submit" onclick="register()" value="Register"></td>
</tr>
</table>
</form>
My Controller for Mysql function:
public function insert()
{
$sql = "INSERT INTO MyGuests (firstname, lastname) VALUES ('".$_REQUEST['fname']."', '".$_REQUEST['lname']."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
}
public function show()
{
$sql = "SELECT * FROM MyGuests";
if ($data = $conn->query($sql) === TRUE) {
echo "New record created successfully";
}
echo "<br><table border='1' align='center'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
foreach($data as $s)
{
echo "<tr><td>".$s->fname."</td>";
echo "<td>".$s->lname."</td></tr>";
}
echo "</table>" ;
}
make sure code is not tested this is the sample for your requirement, aim to aware call immediate function. here i have called view() function just after inserting data in to the database.