I would like to know the best way to delete records from a live database and refresh the page instantly. At the moment I am using ajax, with the following javascript method:
function deleterec(layer, pk) { url = 'get_records.php?cmd=deleterec&pk='+pk+'&sid='+Math.random(); update('Layer2', url); }
if cmd=deleterec on the php page, a delete is done where the primary key = pk. This works fine as in the record is deleted, however the page is not updated.
My update method is pretty simple:
function update(layer, url) { var xmlHttp=GetXmlHttpObject(); //you have this defined elsewhere if(xmlHttp==null) { alert('Your browser is not supported?'); } xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 || xmlHttp.readyState=='complete') { document.getElementById(layer).innerHTML=xmlHttp.responseText; } else if (xmlHttp.readyState==1 || xmlHttp.readyState=='loading') { document.getElementById(layer).innerHTML='loading'; } //etc } xmlHttp.open('GET',url,true); xmlHttp.send(null); }
how to delete or alter record, and upate the page.
At the moment my ajax framework works by passing data to a javascript update method, which works fine for selecting different queries to display in different layers.
I want to add the functionality to delete, or alter the records in a certain way.
I am wondering if it is possible when clicking a link to execute a query and then call my update method and refesh tge page. Is there any easy way to do this given my update methods?
I would like to avoid rewriting my update method if possible.
WOuld the simplest method be to have the php page(only in the layer) reload itself after executing a mysql query?
Or to make a new ‘alterstatus’ method, which would pass delete or watch as a paramter, and have the php execute a query accordingly and then update the page?
edit: The links are generated like so. deleterec would be called from an additional link generated.
{
$pk = $row['ARTICLE_NO']; echo '<tr>' . '\n'; echo '<td><a href='#' onclick='updateByPk(\'Layer2\', \'' . $pk . '\')'>'.$row['USERNAME'].'</a></td>' . '\n'; echo '<td><a href='#' onclick='updateByPk(\'Layer2\', \'' . $pk . '\')'>'.$row['shortDate'].'</a></td>' . '\n'; echo '<td><a href='#' onclick='updateByPk(\'Layer2\', \'' . $pk . '\')'>'.$row['ARTICLE_NAME'].'</a></td>' . '\n'; echo '<td><a href='#' onclick='deleteRec(\'Layer2\', \'' . $pk . '\')'>'.$row['ARTICLE_NAME'].'</a></td>' . '\n'; echo '</tr>' . '\n';
}
edit: the update method can not be modified, as it is used by the updateByPk and updateBypg methods which need a layer.
Without digging too much into your code specifics, I don’t know of any way to update/delete from the server side DB without doing a round trip (either AJAX or a page navigation). I would however recommend using a JavaScript framework (like jQuery, or something else) to handle the AJAX and DOM manipulations. That should, in theory, alleviate any cross-browser troubleshooting on the client side of thinbs.