mcerveni Posted March 30, 2008 Share Posted March 30, 2008 When i delete a record in my table, it will delete always the top record first... for example: my category table results: watch [delete] mantel clock [delete] wall clock [delete] the delete button is a <input type="button" name="btnDeleteCat" value="Delete" onclick="deleteCategory();"> the <input type="hidden" id="rowCatId" value="<?php echo $row['cat_id'];?>"> grabs my id for each record. so when i press delete on Mantel Clock, it will delete Watch, if i press delete on wall clock, it will delete the watch instead. it's always deleting the first record... but when i display that hidden input id, it's retrieving the proper category id. if i hit delete on the watch, it will delete watch. if i hit delete on mantel clock, it will delete mantel clock... so i'm not too sure why it's doing that... here's my ajax and php code: function deleteCategory() { document.getElementById('action_message').innerHTML = '<img src="../images/ajaxLoaderBlack.gif" height="25px"> Deleting Cateogry...'; var catID = document.getElementById('rowCatId').value; nocache = Math.random(); http.open('get', 'delete.php?id='+catID+'&nocache = '+nocache); http.onreadystatechange = deleteReply; http.send(null); } function deleteReply() { if(http.readyState == 4){ var response2 = http.responseText; document.getElementById('insert_response').innerHTML = ' ' +response2+ ''; document.getElementById('action_message').innerHTML = 'The category has been deleted.'; } } php code: $catID = $_GET['id']; $sql = "DELETE FROM catTable where cat_id = '$catID' LIMIT 1 "; $r = mysql_query($sql); //Execute the query. //RELOAD CATEGORY TABLE TO SHOW UPDATES RESULTS. //form tag cannot be here because it wont display the table. $sql = "SELECT * FROM tbl_category"; $r= mysql_query($sql); //Execute the query. //$row= mysql_fetch_array($r); //UPDATE TABLE TO SHOW NEW RESULTS echo '<br><br>'; showCatResults_Ajax(); -Thanks in advance, Mike Quote Link to comment Share on other sites More sharing options...
AP81 Posted April 2, 2008 Share Posted April 2, 2008 I believe your problem is with this line: <input type="hidden" id="rowCatId" value="<?php echo $row['cat_id'];?>"> If you look at the above "id" will not be unique if you have more that one of these. There for document.getElementById('rowCatId').value will return the first "id" it finds, which is why you always end up deleting the first one. Rather than using a hidden field, I'd put the id into the button and do something like this: <input type="button" value="Delete" id="<?php echo $row['cat_id'];?>" onclick="deleteCategory(this.id);"> When you click this button, it will pass the id to the deleteCategory function. So you need to edit your deleteCategory function like this: function deleteCategory(sID) { ... http.open('get', 'delete.php?id='+sID+'&nocache = '+nocache); ... } Hope this helps! Quote Link to comment Share on other sites More sharing options...
mcerveni Posted April 2, 2008 Author Share Posted April 2, 2008 Thanks so much! i was stuck on that forever... i'll try that tonight... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.