delete record in a table using jquery ajax

5cott226

New Member
I am learning j query Ajax events.I have inserted and displayed records in php using j query Ajax but I am not able to delete record I have written code but its not working and I don't want table to load again.Please helpcomment.php\[code\]<script type="text/javascript">$(document).ready(function(){ //Display Records function showComment(){ $.ajax({ type:"post", url:"process.php", data:"action=showcomment", success:function(data){ $("#comment").html(data); } }); }showComment(); //Insert records $("#Submit").click(function(){ var name=$("#name").val(); var message=$("#message").val(); $.ajax({ type:"post", url:"process.php", data:"name="+name+"&message="+message+"&action=addcomment", success:function(data){ showComment(); } }); });//Delete records $(".delete_button").click(function() {var id = $(this).attr("id");var dataString = 'id='+ id ;var parent = $(this).parent();$.ajax({ type: "POST", url: "delete.php", data: dataString, cache: false, success: function() { if(id % 2) { parent.fadeOut('slow', function() {$(this).remove();}); } else { parent.slideUp('slow', function() {$(this).remove();}); } } });return false; });}); </script></head><body><form id="myform"> Name : <input type="text" name="name" id="name"/> </br> Message : <input type="text" name="message" id="message" /> </br> <input type="button" value="http://stackoverflow.com/questions/15917833/Submit" id="Submit"> </br> </br> </br> </br> <div id="comment"></div> </form></body>\[/code\]process.php\[code\]<?php mysql_connect("localhost","root","dot1235"); mysql_select_db("chitra"); $action=$_POST["action"]; if($action=="showcomment"){ $show=mysql_query("Select * from login order by mid ASC"); ?> <table border=1 id=t1> <tr> <td>SrNo.</td> <td>Name</td> <td>Message</td><td>Delete</td> </tr><?php $i=1; while($row=mysql_fetch_array($show)){ ?> <tr> <td><?php echo $i; ?></td> <td><?php echo $row['username']; ?></td> <td><?php echo $row['message']; ?></td> <td><a href="http://stackoverflow.com/questions/15917833/#" id="<?php echo $mid; ?>" class="delete_button">Delete</a></td> </tr> <?php $i++; } ?> </table> <?php } else if($action=="addcomment"){ $name=$_POST["name"]; $message=$_POST["message"]; $query=mysql_query("insert into login(username,message) values('$name','$message') "); if($query){ "Your comment has been sent"; } else{ "Error in sending your comment"; } }?>\[/code\]delete.php\[code\]<?phpinclude("connection.php"); if(isset($_POST['id'])) { $id = $_POST['id']; $id = mysql_escape_String($id); $delquery=mysql_query("delete from login where mid=$id") or die(mysql_error()); //echo "Record deleted"; }?>\[/code\]
 
Top