php - to display all records using jquery ajax -
i have written code displaying records on same page not working.actually want in page insert same page should show records in table format , show added record without refreshing page.
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 mysql</a>--> <!-- displaying message --> <div id="message"></div> </form> </body>
demo_insert.php
<? include('connection.php'); //pull data 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 mysql $query=mysql_query("insert school(name,rno,address) values('$name','$rno','$address')"); $dispquery=mysql_query("select * 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>"; ?>
first off, please use mysqli or pdo connect database, mysql methods deprecated , stop working sometime , break code.
secondly, way there, though not quite. suggest taking different approach, , perhaps make use of jquery , json.
change first file this:
<script type="text/javascript" > $(document).ready(function() { $.ajax({ type:'post', data: { name: $("#name").val(), rno: $("#rno").val(), address: $("#address").val() }, datatype: 'json', url:'demo_insert.php', success: function(data) { if(data.insert) { alert("insertion success"); // content in data.output can append wherever } else { alert("not inserted"); } } }); }); </script>
and php such, please note not changing code pdo or mysqli, should that!
<?php include('connection.php'); //pull data home.php front-end page $response = new stdclass(); $name=mysql_escape_string($_post['name']); $rno=mysql_escape_string($_post['rno']); $address=mysql_escape_string($_post['address']); //insert data mysql $query=mysql_query("insert school(name,rno,address) values('$name','$rno','$address')"); // add insert response $response->insert = !$query ? false : true; // continue if true if ($response->insert) { $dispquery=mysql_query("select * school"); $response->output = ''; $response->output .= " <table border=1> <tr> <td>srno</td> <td>name</td> <td>rno</td> <td>address</td> </tr>"; $i=1; while($result=mysql_fetch_array($dispquery)) { $response->output .= " <tr> <td>$i</td> <td>{$result['name']}</td> <td>{$result['rno']}</td> <td>{$result['address']}</td> </tr>"; $i++; } $response->output .= "</table>"; } header('content-type: application/json'); echo json_encode($response);
?>
Comments
Post a Comment