我已经编写了用于在同一页面上显示记录的代码,但它不起作用。实际上,我希望在插入同一页面的页面中以表格格式显示所有记录,并且还显示最近添加的记录而不刷新页面。
ajaxinsert.php
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript" >
$(document).ready(function() {
$("#insert").click(function() {
var name = $("#name").val();
var rno = $("#rno").val();
var address = $("#address").val();
//var dataString = "&name="+name"+&address="+address;
$.ajax({
type:'POST',
data:{name: name,
rno:rno,
address:address},
url:'demo_insert.php',
success:function(data){
if(data="inserted") {
alert("Insertion Success");
} else {
alert("Not Inserted");
}
}
});
});
});
</script>
</head>
<body>
<form id="myform" action="demo_insert.php">
<label>Name: </label> <input id="name" type="text" />
<label>Rno: </label> <input id="rno" type="text" />
<label>Address: </label> <input id="address" type="text" />
<input name="Submit" type="button" id="insert" value="Submit"/>
<!--<a id="insert" title="Insert Data" href="#">Push into mysql</a>-->
<!-- For displaying a message -->
<div id="message"></div>
</form>
</body>
demo_insert.php
<?
include('connection.php');
//Pull data from home.php front-end page
$name=mysql_escape_string($_POST['name']);
$rno=mysql_escape_string($_POST['rno']);
$address=mysql_escape_string($_POST['address']);
//Insert Data into mysql
$query=mysql_query("INSERT INTO school(name,rno,address) VALUES('$name','$rno','$address')");
$dispquery=mysql_query("select * from school");
echo "<table border=1>";
echo "<tr>";
echo"<td>Srno</td>";
echo "<td>Name</td>";
echo"<td>Rno</td>";
echo "<td>Address</td>";
echo"</tr>";
$i=1;
while($result=mysql_fetch_array($dispquery))
{
echo "<tr>";
echo "<td>" .$i."</td>";
echo "<td>" .$result['name'] ."</td>";
echo "<td>" .$result['rno'] ."</td>";
echo "<td>" .$result['address'] ."</td>";
echo "</tr>";
$i++;
}
echo"</table>";
?>