Prototip js kullanarak php silme işlemi

1 Cevap php

Ben bir tablodan kayıt silmek için php / mysql ve protype.js kullanıyorum. Sorun ızgara kayıt sildikten sonra, ızgara şablonuna otomatik güncelleme olmasıdır.

Bu benim kodudur:

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 Cevap

Bunu prototip ile bu tablo satırı kaldırabilirsiniz:

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