views:

309

answers:

2

I have code to delete a record from mysql, displayed in a table via php, and subsequently delete the table row from the page. The record is deleted, however nothing changes in the page or the DOM, and it should change instantly.

Here is the javascript code to delete from the DOM

function deleteItem(layer, url) {
    var xmlHttp=GetXmlHttpObject();
    if(xmlHttp==null) {
        alert("Your browser is not supported?");
    }
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
                if(xmlHttp.responseText == 'result=true') {
                        var row = document.getElementById(layer);
                        row.parentNode.removeChild(row);
                }

        } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
                document.getElementById(layer).innerHTML="loading";
        }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}
function deleteRec(layer, pk) {
    url = "get_records.php?cmd=deleterec&pk="+pk+"&sid="+Math.random();
    if (confirm("Confirm Delete?")) {
        deleteItem(layer, url);
    }
}

Which is called from php like so:

echo '<tr id=\"article_' . $pk . '\">' . "\n";   
echo '<td><a href="#" onclick="deleteRec(\'article_' . $pk .'\', \'' . $pk . '\')">DELETE</a></td>' . "\n";

$pk is the primary key of the record.

The resultant html is fine(obviously since the record is deleted)

<a href="#" onclick="deleteRec('article_260343387801', '260343387801')">DELETE</a>

But the page is not updated in any way.

Why?

+1  A: 

I don't immediately see why:

row.parentNode.removeChild(row);

wouldn't work... are you sure you're actually getting there? Your error reporting is problematic:

document.getElementById(layer).innerHTML=xmlHttp.responseText;

‘layer’ is a <tr>, and you can't assign innerHTML on a <tr> in IE. Instead, you will get an ‘Unknown runtime error’ exception and the script will stop.

(Plus, it's undefined what would happen if you tried to put text directly inside a <tr> without a <td> around it.)

bobince
AHh, no I am not getting there. I placed an alert before declaring var row, and it was not displayed, yet the record is deleted. What would cause it to not go that far ahead?
Joshxtothe4
what does this show: alert(xmlHttp.responseText), just before the first if?
flybywire
I mean, just before the first if of onreadystatechange
flybywire
"row.parentNode.removeChild(row);" will absolutely work btw, at least in principle
annakata
andy, no alert is output at all.
Joshxtothe4
You should try to type the url directly in your navigator and see what the script returns (not using javascript), if it does not output "result=true" then your problem probably comes from your php script and not from the javascript
Vinze
I have : if(cmd="deleterec"){do mysql delete, return"result=true";} which works. the outout of responseText is blank...
Joshxtothe4
+3  A: 

I just threw your code into a test page and it works for me when I remove the:

 document.getElementById(layer).innerHTML=xmlHttp.responseText;

line that's just before the line:

} else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {

The document.getElementById(layer).innerHTML=xmlHttp.responseText; is causing an error.

Edit: I'll just add that my 'version' of get_records.php is simply a php page the echos 'result=true' for my own testing purposes, so if you're still running into issues then I would suggest that your PHP script, while deleting the correct data, isn't returning the correct string - you should check what actually gets output to xmlHttp.responseText

Edit 2: Your PHP code never actually returns 'result=true' in a manner which your responseText will recognise. You have:

if($cmd=="deleterec") {
    mysql_query('DELETE FROM AUCTIONS WHERE ARTICLE_NO = ' . $pk);
}

so you will delete the correct item but you have no echo 'result=true'; anywhere so your if statement checking for responseText is never going to get called.

EDIT 3: My current test code (which works fine in firefox [untested in other browsers]).

<script type="text/javascript">
    var xmlHttp;

    function GetXmlHttpObject(){
        var objXMLHttp = null;

        if (window.XMLHttpRequest){
        try{
            objXMLHttp = new XMLHttpRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.createRequest){
        try{
            objXMLHttp = new window.createRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.ActiveXObject){
        try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                objXMLHttp = false;
            }
        }
    }

    return objXMLHttp;
  }
 function deleteItem(layer, url) {
 var xmlHttp=GetXmlHttpObject();
 if(xmlHttp==null) {
    alert("Your browser is not supported?");
 }
 xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            console.log(xmlHttp.responseText);
            if(xmlHttp.responseText == 'result=true') {
                    var row = document.getElementById(layer);
                    row.parentNode.removeChild(row);
            }
            //document.getElementById(layer).innerHTML=xmlHttp.responseText;
    } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
            //document.getElementById(layer).innerHTML="loading";
    }
 }
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
 }
 function deleteRec(layer, pk) {
    url = "get_records.php?cmd=deleterec&pk="+pk+"&sid="+Math.random();
    if (confirm("Confirm Delete?")) {
    deleteItem(layer, url);
    }
 }
</script>


<table>
      <tr id="article_260343387801">
      <td><a href="#" onclick="updateByPk()">Name</a></td>
      <td><a href="#" onclick="deleteRec('article_260343387801', '260343387801')">DELETE</a>
      </td>
      </tr>
 </table>

This works fine in combination with the php code (with the database connection/query stuff commented out for my personal testing).

Steerpike
that innerHTML line is going to break if result=true because the block above is going to remove the same element from the DOM
annakata
Yeah, that's what made me add the edit - even with the line in my test page still deletes the node and then errors trying to add text into that same node. Which is what makes me think the error is in the php return value. Not sure why I got -1 *shrug*
Steerpike
Christ knows, everybody here did - I guess the OP is downvoting things which aren't "correct". I reckon the return value is probably not as anticipated, but we can't tell can we :)
annakata
I had actually allready removed the line you describe, it was not meant to be in my posting. Deleting works fine, just not updating the DOM. I have put my get_records here http://pastebin.com/d5fecacfe but I don't think that is the problem.
Joshxtothe4
I didnt downvote anyone, I am upvoting you all now.
Joshxtothe4
for the "loading" message, adding an id to the td and changing it's innerHTML should do what you want... Even if it won't solve the main problem :(
Vinze
steerpike, I did add return "result=true"; however this does not change anything. alert(xmlHttp.responseText); is displayed, but is blank.
Joshxtothe4
That's because return "result=true"; isn't what you want. You want echo "result=true"; responseText is the full text of the page that gets outputted by the script - therefore your *entire* response page should consist *only* of 'result=true' echoed to the page
Steerpike
I tried with echo, and there was no change. responseText was still blank.
Joshxtothe4
Joshxtothe4
Can you post updated code then? When I use the php code you've posted I get the pagination code printed to the page in addition to the echo I added.
Steerpike
My consloe.log gets: result=true</div><div id="tablefooter" class="tablefooter"><div id="tablefooterleft" class="tablefooterleft"><a href="#" onclick="updateByPage('Layer3', '', '1')"> <<-First </a><a href="#" onclick="updateByPage('Layer3', '', '1')"> <-Previous</a>
Steerpike
-----------------------------------------------------------------<a href="#" onclick="updateByPage('Layer3', '', '')"> Next -> </a><a href="#" onclick="updateByPage('Layer3', '', '')"> Last ->></a></div></div>
Steerpike
http://pastebin.com/d8748e1b
Joshxtothe4
Well, once you fix the syntax error on line 79 (where you comment out a { that you need) that code should work fine. It works perfectly on my test setu
Steerpike
I don't have a syntax error, pastebin changed things around. I have made the file available here: http://www.sendspace.com/file/wdo99f and it seems to get to the level to remove the row, but gives an error that row is null.
Joshxtothe4
hmm, in firebug I just get row is nullonreadystatechange()()fetchlayers.js (line 56)[Break on this error] row.parentNode.removeChild(row);
Joshxtothe4
This is the entire code I am using: http://pastebin.com/d46480fd0There are no syntax errors, if there are it is due to formatting for pastebin.
Joshxtothe4
Using that code you just posted to pastebin works fine for me with the php file you supplied as well. Sorry but I now have officially no idea what might be going wrong - unless you have too many elements with the same id in your html or something like that?
Steerpike
hmm, every id is unique, as it comes from the primary key.
Joshxtothe4
Then all I can suggest is make sure your script is outputting valid html, because as far as I can tell that's the only part that's different between us as things stand.
Steerpike
I will experiment further. Thankyou for your help thus far.
Joshxtothe4
I fixed it, it was indeed a html problem.
Joshxtothe4