I am using prototype js with PHP and mysql for update the status of record enable to disable vice versa in a grid. For this i use Ajax.Request method of prototype. status is updating but status icon was not change.
I am using image for showing enable or disable record. if record is enable in a grid then you can see enable.png image in a row of perticular record. Then i am changing the status of a perticular record in a grid, then status image should be changed with disable.png image in a grid for perticular record. In back end status has been changed but according to the current status image is not changing. My code is:
Step 1.
<?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 src="scriptaculous-js/src/unittest.js" type="text/javascript"></script>
<script language="javascript">
function fnDeleteRecord(id)
{
new Ajax.Request('delete.php?action=Delete&id='+id,{method:'get'});
$(id).remove();
}
function fnUpdateStatus(id,value)
{
//new Ajax.Request('delete.php?action=Update&id='+id+'&status='+value, {method:'get',frequency:5,decay:3, onCreate: Ajax.Responders.register({onCreate: fnShowProcess(), onComplete:fnHideProcess()}), onSuccess:fnUpdateRecord});
new Ajax.Request('delete.php?action=Update&id='+id+'&status='+value, {method:'get'});
}
function fnShowProcess()
{
var i = 0;
if(Ajax.activeRequestCount > 0)
document.getElementById('a[i]').style.display = 'inline';
}
function fnHideProcess()
{
var i=0;
if(Ajax.activeRequestCount <= 0)
document.getElementById('a[i]').style.display='none';
}
function fnUpdateRecord()
{
new Ajax.Updater('userrecords', 'index.php', {method:'post'});
}
</script>
<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);
?>
Step 2.
<?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;
case 'Update':
echo $sql = "UPDATE tbluser SET status = '".$_GET['status']."' WHERE id = '".$_GET['id']."'";
$result = mysql_query($sql);
if(!$result)
echo 'some problem occured on server during update operation';
break;
}
}
?>