Forum Moderators: open

Message Too Old, No Replies

running ajax/javascript outputed by ajax?

         

heinz1218

4:21 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



A person submits a reply to a message (think forum-style), and it calls an ajax function that inserts into mysql db and then comes back "reply submitted." Instead of making people click on a link that will then display all replies (another ajax function), i'd like for it to wait say two seconds and then run the ajax function which displays replies. I tried outputting
<script>showReply()</script>
in my PHP page, but it doesn't work. I also tried calling the 2nd ajax function within the first function's javascript, but that loads the entire website w/in the website. Here's the two fucntions:

function submitReply2(objID,msgID) { //make reply
var obj = document.getElementById(objID);
//var thismsgID = msgID;
xmlhttp.open("POST","?submitReply2");
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText; //use for debugging
}
}
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('subnum='+msgID+'&opinionID='+document.replyaddform.opinionID.value+'&replytitle='+document.replyaddform.replytitle.value+'&replytext='+document.replyaddform.replytext.value);
return false;
}

function showReply(objID,msgID) {
var obj = document.getElementById(objID);
//var thismsgID = msgID;
xmlhttp.open("POST","?showReply");
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText; //use for debugging
}
}
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('msgID='+msgID);
return false;
}

StupidScript

4:53 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When the response comes back "reply submitted", include a little Javascript:
setTimeout('showReply(yada,yada)',2000);

If showReply() is passed the proper arguments, it should do its thang, just like clicking on a link to do it would.

heinz1218

5:43 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



Yeah sorry didn't mention, I already tried that. So here's my php:

echo "<br><strong>Reply Submitted.</strong><a href=\"?\" onclick=\"return showReply('quote_reply_container_".$opinionID."','".$opinionID."')\">Refresh Link</a>";
echo "<script>setTimeout(showReply('quote_reply_container_".$opinionID."','".$opinionID."'),2000);</script>";

which results in the folowing sent to the page (i checked w/Firebug):
<script>setTimeout(showReply('quote_reply_container_103','103'),2000);</script>

The link works, but it doesn't execute the Javascript. Just to test it i tried doing:

echo "<script>alert('hi')</script>";

but still didn't work.

cmarshall

6:40 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't write JavaScript as HTML content, and execute it. I already ran into that.

You actually need to do DOM construction.

Here are a couple of threads you might want to look at:

[webmasterworld.com ]

[webmasterworld.com ]