使用原型js在php中删除操作

时间:2009-06-03 17:15:07

标签: php javascript prototypejs

我使用php / mysql和protype.js从表中删除记录。问题是从网格中删除记录后,网格不会在模板中自动更新。

这是我的代码:

第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>

第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 个答案:

答案 0 :(得分:1)

您可以使用原型删除该表行:

<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>