Note that there are some explanatory texts on larger screens.

plurals
  1. POdeleting mysql records with ajax
    text
    copied!<p>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:</p> <pre><code>function deleterec(layer, pk) { url = "get_records.php?cmd=deleterec&amp;pk="+pk+"&amp;sid="+Math.random(); update('Layer2', url); } </code></pre> <p>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.</p> <p>My update method is pretty simple:</p> <pre><code>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); } </code></pre> <p>how to delete or alter record, and upate the page.</p> <p>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. </p> <p>I want to add the functionality to delete, or alter the records in a certain way.</p> <p>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?</p> <p>I would like to avoid rewriting my update method if possible. </p> <p>WOuld the simplest method be to have the php page(only in the layer) reload itself after executing a mysql query?</p> <p>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?</p> <p>edit: The links are generated like so. deleterec would be called from an additional link generated.</p> <p>{</p> <pre><code>$pk = $row['ARTICLE_NO']; echo '&lt;tr&gt;' . "\n"; echo '&lt;td&gt;&lt;a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')"&gt;'.$row['USERNAME'].'&lt;/a&gt;&lt;/td&gt;' . "\n"; echo '&lt;td&gt;&lt;a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')"&gt;'.$row['shortDate'].'&lt;/a&gt;&lt;/td&gt;' . "\n"; echo '&lt;td&gt;&lt;a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')"&gt;'.$row['ARTICLE_NAME'].'&lt;/a&gt;&lt;/td&gt;' . "\n"; echo '&lt;td&gt;&lt;a href="#" onclick="deleteRec(\'Layer2\', \'' . $pk . '\')"&gt;'.$row['ARTICLE_NAME'].'&lt;/a&gt;&lt;/td&gt;' . "\n"; echo '&lt;/tr&gt;' . "\n"; </code></pre> <p>}</p> <p>edit: the update method can not be modified, as it is used by the updateByPk and updateBypg methods which need a layer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload