views:

791

answers:

4

I am using php / mysql and protype.js to delete record from a table. problem is that after deleting record from grid, grid is not updating automatically in template.

This is my code:

Step 1: index.php

<?php
    require("Connection.php");
    $sql = "SELECT * from tbluser";
    $result=mysql_query($sql);
?>
<html>
<head>
    <script src="prototype.js" type="text/javascript"></script>
    <script src="scriptaculous-js/src/scriptaculous.js" type="text/javascript"></script> 
    <script language="javascript">
     function fnDeleteRecord(id)
     {
         new Ajax.Request('delete.php?action=Delete&id='+id,{method:'get'});
     }
    </script>
</head>
<body>
<table id="userrecords" border="1" cellpadding="0" cellspacing="0" width="700">
    <tr>
     <th> User Name </th>

     <th> Password </th>

     <th> Address </th>

     <th> Phone Number </th>

     <th> Action </th>
     <th> Status </th>
    </tr>
<?php

while($row = mysql_fetch_array($result))
{
    echo "<tr id='".$row[id]."'>";

    echo "<td>".$row['user_name']."</td>";

    echo "<td>".$row['pass']."</td>";

    echo "<td>".$row['address']."</td>";

    echo "<td>".$row['phone_no']."</td>";

    echo "<td><img src='images/spinner.gif' id='a[$row[id]]' alt=icon border=0 /><a href='javascript:void(0);' onclick=fnDeleteRecord('".$row['id']."'); >Delete</a></td>";

    if($row['status'] == 'enabled')
     echo "<td><img src='images/spinner.gif' id='a[$row[id]]' alt=icon border=0 style='display:none;' /><img src='images/enable.png' alt=icon border=0 onClick=fnUpdateStatus('".$row['id']."','disabled')  /></td>";
    else if ($row['status'] == 'disabled')
     echo "<td><img src='images/spinner.gif' id='a[$row[id]]' alt=icon border=0 style='display:none;' /><img src='images/disable.png' alt=icon border=0 onClick=fnUpdateStatus('".$row['id']."','enabled') /></td>";

    echo "</tr>";
}
    echo "</table>";

    mysql_close($con);

?>
</body>
</html>

Step 2: delete.php

<?php
require_once('connection.php');
if ($_GET['action'])
{
    switch($_GET['action'])
    {
     case 'Delete':
      $sql = "Delete from tbluser where id ='".$_GET['id']."'";
      $result = mysql_query($sql);
      if(!$result)
       echo "some problem occured during delete operation";
      break;
    }
}
?>
+1  A: 

You can remove that table row with prototype:

<script language="javascript">
   function fnDeleteRecord(id)
   {
       new Ajax.Request('delete.php?action=Delete&id='+id,{method:'get'});
       $(id).remove(); // because <tr id='".$row[id]."'> :)

   }
</script>
sasa
Much better answer than mine +1
ichiban
A: 

Thanks it's nice solution. Before this answer i was use Ajax.updater method onSuccess By which i am getting updateed record in table but using this technique i have reloaded header on the template. that's why i am asking another technique for doing this.

Thanks Asheesh

A: 

I have used the above method but I keep on receiving an error when it comes to the line $(id).remove();

Apparently the id being passed is null.

Can anyone please help me with this

Thanks Mario

A: 

@Mario

If your id is an integer convert it to a string. $(id+'').