Utherr12 Posted December 5, 2010 Share Posted December 5, 2010 ok... so i have something like <div id="replaced"><span id="replace"><p id="content" class="body">$comments_content</p><a style="font-size:x-small" href="java script:addCom()">[Modify]</a></span></div> The code of addCom(): To make this short, this code replaces the innerHTML of "replaced" with a textarea that contains the previous $comments_content. And adds a button which on click calls a AJAX function. var Content = document.getElementById('content').innerHTML; var replace_span = document.getElementById('replace'); var textarea = document.createElement('textarea'); document.getElementById('replaced').replaceChild(textarea,replace_span); document.getElementsByTagName('textarea').item(1).id = "comm"; var textarea_tag = document.getElementById('comm'); textarea_tag.cols = 45; textarea_tag.rows = 15; textarea_tag.innerHTML = Content; var brk = document.createElement('br'); var button = document.createElement('button'); button.addEventListener("click",sendAjax,false); button.innerHTML = "Modify"; document.getElementById('replaced').appendChild(brk); document.getElementById('replaced').appendChild(brk); document.getElementById('replaced').appendChild(button); Here's the ajax code: function sendAjax(){ Ajax = newAjax(); if((Ajax.readyState==4)||(Ajax.readyState==0)) { var Content = document.getElementById('comm').innerHTML; var Cid = document.getElementById('cid').value; Ajax.open("POST","editComm.php?cid="+Cid, true); Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); Ajax.onreadystatechange = function() { if(Ajax.readyState==4) { content_response = Ajax.responseText; alert(Ajax.responseText); document.getElementById('replaced').innerHTML = "<span id=\"replace\"><p id=\"content\" class=\"body\">"+content_response+"</p><a style=\"font-size:x-small\" href=\"java script:addCom()\">[Modify]</a></span>"; } } Ajax.send("content="+Content); }} Where's the problem here? Well this line: var Content = document.getElementById('comm').innerHTML; takes the innerhtml of "comm" from when I first created it by addComm() function it doesnt change no matter what i type.It's like this: i press addComm() textarea with the previous content and button appears. I type something, Content remains the same.How do I get around this ? Link to comment Share on other sites More sharing options...
jeffman Posted December 5, 2010 Share Posted December 5, 2010 You mean after you click the button to start the AJAX function? What is supposed to happen? You have not said anything about editComm.php and what it's supposed to send back.I had to change this: document.getElementsByTagName('textarea').item(1).idto this: document.getElementsByTagName('textarea')[0].idto make it work in Firefox.Then I just set up a dummy script that echoes "Hello," and the original text was replaced by "Hello".Is something else supposed to happen?Is the value of Cid important to my understanding anything?EDITED [0] mistake. Link to comment Share on other sites More sharing options...
Utherr12 Posted December 5, 2010 Author Share Posted December 5, 2010 $new_content = $_POST['content'];$escaped_content = mysql_real_escape_string($new_content);$stripped_content = htmlspecialchars($escaped_content);$cid = $_GET['cid'];$update_comment = mysql_query("UPDATE `comments` SET `content` = '$stripped_content' WHERE `id` = '$cid';");echo $new_content;but it i dont think it's relevant.... http://89.115.144.183/blogger/index.php?postID=9 try it hereThe post doesn't modifies because the function keeps getting the same content because var Content = document.getElementById('comm').innerHTML; remains the same with the first textarea i created (with the original content). Link to comment Share on other sites More sharing options...
jeffman Posted December 6, 2010 Share Posted December 6, 2010 I almost spotted this before, but then I forgot.Change this:var Content = document.getElementById('comm').innerHTML;to this:var Content = document.getElementById('comm').value;It's not intuitive that a textarea has a value property, since it looks like the text is innerHTML. In some browsers innerHTML works. In others, the innerHTML remains the way it was when the page first loads, kind of like defaultValue. But the value property is the W3C standard, and it works everywhere. Link to comment Share on other sites More sharing options...
Utherr12 Posted December 6, 2010 Author Share Posted December 6, 2010 IT WORKED FINNALLY....i had to change index(1) to [1] and innerHTML to value you pointed out. Link to comment Share on other sites More sharing options...
boen_robot Posted December 6, 2010 Share Posted December 6, 2010 You might want to do htmlspecialchars() on the response, or better yet, don't use innerHTML to put the text in, but set the span's nodeValue instead.The reason is that currently, after you hit "Modify", HTML gets interpreted once. It doesn't persist thanks to your... better... usage of htmlspecialchars() in the script, but it's annoying to see fancy formatting only to see it go after a refresh. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.