Jump to content

ajax function doesnt run


etsted

Recommended Posts

function loadXMLDoc(url, cfunc){ xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=cfunc; xmlhttp.open("POST",url,true); xmlhttp.send("action=delete_reply&replyid="+replyid+"&document="+document);}function deleteReply(replyid,replybox,document){loadXMLDoc("php_parsers/status_system2.php",function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { _(replybox).style.display = 'none'; } else { alert(ajax.responseText); } });}

Link to comment
Share on other sites

what's the context? is deleteReply being called? are you checking for errors? doing any logging / debugging? you should be looking in your console for these and any logging you need to add, it will help you figure out how far your code runs and if it's breaking, it will show you the error and the line.

 

one thing that looks odd is this line

 

 _(replybox).style.display = 'none';

 

it doesn't look syntactically correct, which you would be able to confirm if you were looking in the error console of your browser.

Link to comment
Share on other sites

where is it defined? I see where it's used, but like I said, you provided very little context and you didn't even provide context for the error (line number or line of code the error referenced).

 

I would bet 100% that if the console says it's not defined, then it's not defined. I would provide more code so we can actually see the scope you say you've defined replyid in, but I am sure that the issue is that, unless it's a global, it's not going to work the way you expect it to in loadXMLDoc.

Link to comment
Share on other sites

<?php  $sql = "SELECT * FROM $DB_table WHERE osid='$statusid' AND type='b' ORDER BY postdate ASC";    $query_replies = mysqli_query($con, $sql);    $replynumrows = mysqli_num_rows($query_replies);    if($replynumrows > 0){                while ($row2 = mysqli_fetch_array($query_replies, MYSQLI_ASSOC)) {                        $statusreplyid = $row2["id"];            $replyauthor = $row2["author"];            $replydata = $row2["data"];            $replydata = nl2br($replydata);            $replypostdate = $row2["postdate"];            $replydata = str_replace("&","&",$replydata);            $replydata = stripslashes($replydata);                        $replyDeleteButton = '';                        // gives the author of the comment, or the page owner the ability to delete the reply            if($replyauthor == $log_username || $account_name == $log_username ){                $replyDeleteButton = '<span id="srdb_'.$statusreplyid.'"><a href="#" onclick="return false;" onmousedown="deleteReply(''.$statusreplyid.'','reply_'.$statusreplyid.'',''.$DB_table.'');" title="DELETE THIS COMMENT">remove</a></span>';            }                        // holds all of the replies            $status_replies .= '<div id="reply_'.$statusreplyid.'" class="reply_boxes"><div><b>Reply by <a href="user.php?u='.$replyauthor.'">'.$replyauthor.'</a> '.$replypostdate.':</b> '.$replyDeleteButton.'<br />'.$replydata.'</div></div>';                }                }?>
Link to comment
Share on other sites

There is nothing to show becuase all you have done is store the content in variable $replyDeleteButton, you are not displaying it with echo, unless you just have NOT shown us that part again!.

Edited by dsonesuk
Link to comment
Share on other sites

ok, so then you figured out your other issues then? I'm really having a hard time following along with what you actually need help with.

 

If you are able to make the request, but not getting a success callback, then you need to look in the networks tabs of your console and look at the response to see what you're sending, and what the server is returning.

Link to comment
Share on other sites

IT also maybe down to the fact you are adding inline javascript from an returned php, AFTER the main page has loaded, so it has no reference to run these new JavaScript functions.

 

EDIT: with jquery you would use live() (its change now to anther function name) to keep newly added JavaScript running after the page has loaded.

Edited by dsonesuk
Link to comment
Share on other sites

It seems to me that you are writing a lot of code that does not check for unexpected conditions or report errors. You need an error reporting scheme.

Link to comment
Share on other sites

this is the content of the comments.

 

<div id="status_75" class="status_boxes">

<div>

<b>Posted by <a href="user.php?u=jacop">jacop</a> 2014-05-08 20:34:11:</b>

<span id="sdb_75"><a href="#" onclick="return false;" onmousedown="deleteStatus('75','status_75','comments');" title="DELETE THIS STATUS AND ITS REPLIES">delete status</a></span>

&nbsp; &nbsp;

<br />hei</div>

</div>

<textarea id="replytext_75" class="replytext" onkeyup="statusMax(this,250)" placeholder="write a comment here"></textarea><button id="replyBtn_75" onclick="replyToStatus(75,'george','replytext_75',this,'comments')">Reply</button>

Link to comment
Share on other sites

But what do you do with the variable $status_replies where you have built a long innerHTML string which contains the call to deleteReply().?

Link to comment
Share on other sites

Yes, but you always show only part of the code and you never explain what file it is from. You show the creation of the $status_replies string but you don't test every variable that you insert into that string to see if it is valid and you don't echo that string to see if it looks valid.

Link to comment
Share on other sites

well i was viewing the source code after posting a comment, and the source didnt macht with what was stored in the DB, then i refreshed the view source code, and suddently the code was machting the DB, any suggestions?

Link to comment
Share on other sites

so now i have tried to use jQuerys function $(document).ready function, it didnt work, i used the console.log, and saw that postToStatus was not defined...

 

$(document).ready(function(){ $("#statusBtn").click(function(){function postToStatus(action,type,user,ta,document){ // store the value of the thread var data = _(ta).value; /*if(data === ""){ alert("Type something first"); return false; }*/ _("statusBtn").disabled = true; var ajax = ajaxObj("POST", "php_parsers/status_system2.php"); ajax.onreadystatechange = function() { if(ajaxReturn(ajax) === true) { // split the incoming string, to get the last inserted id var datArray = ajax.responseText.split("|"); if(datArray[0] === "post_ok"){ // the inserted id var sid = datArray[1]; data = data.replace(/</g,"<").replace(/>/g,">").replace(/n/g,"<br />").replace(/r/g,"<br />"); // holds all the threads and replies var currentHTML = _("statusarea").innerHTML; _("statusarea").innerHTML = '<div id="status_'+sid+'" class="status_boxes"><div><b>Posted by you just now:</b> <span id="sdb_'+sid+'"><a href="#" onclick="return false;" onmousedown="deleteStatus(''+sid+'','status_'+sid+'');" title="DELETE THIS STATUS AND ITS REPLIES">delete status</a></span><br />'+data+'</div></div><textarea id="replytext_'+sid+'" class="replytext" onkeyup="statusMax(this,250)" placeholder="write a comment here"></textarea><button id="replyBtn_'+sid+'" onclick="replyToStatus('+sid+','<?php echo $u; ?>','replytext_'+sid+'',this)">Reply</button>'+currentHTML; _("statusBtn").disabled = false; _(ta).value = ""; } else { alert(ajax.responseText); } } }; ajax.send("action="+action+"&type="+type+"&user="+user+"&data="+data+"&document="+document);} });});

Link to comment
Share on other sites

this is the button i am using

 

$status_ui = '<textarea id="statustext" onkeyup="statusMax(this,250)" placeholder="'.$u.'"></textarea>'; $status_ui .= '<button id="statusBtn" onclick="postToStatus('status_post','a',''.$u.'','statustext',''.$DB_table.'')">Post</button>';

Link to comment
Share on other sites

What's wrong is that you still don't want to use code tags. When I open a topic by etsted, and I see a bunch of code without code tags, I skip it. If you're not going to try and make it easy for me to help you, then I'm not going to try and help you.The other problem is that you don't seem to understand what you're doing. You have this line:

$status_ui .= '<button id="statusBtn" onclick="postToStatus('status_post','a',''.$u.'','statustext',''.$DB_table.'')">Post</button>';
So, you're assigning a click handler to that button. But then you have this:
$(document).ready(function(){    $("#statusBtn").click(function(){function postToStatus(action,type,user,ta,document){
So now, instead of putting the click handler in the onclick attribute, now you're trying to use jQuery to assign a click handler. And, inside that click handler, you define the function that you use in the onclick attribute. That means that the function you're telling it to use to handle the click event doesn't even get defined until you click on it, and even then it only gets defined locally.If you want to use the onclick attribute to define click handlers, then use globally-defined functions for those. If you want to use jQuery to define a click handler, then don't use the onclick attribute.And, whatever you do, put each of your sections of code inside a code block. That doesn't mean you put a bunch of pieces of code from different files in the same block. If you have code from 3 files, then use 3 code blocks. It's not difficult to understand. I'm not going to deal with this again, if I open one of your topics and see a wall of code just pasted in, without a code block, I'm going to ignore it.
Link to comment
Share on other sites

 

so now i have tried to use jQuerys function $(document).ready function, it didnt work, i used the console.log, and saw that postToStatus was not defined...

 

Well, I don't see any need to mix jQuery into this. What problem do you think jQuery is going to solve? When you say "postToStatus() was not defined" you do not explain where postToStatus() is supposed to be defined. You keep showing us code but you do not explain what we are looking at, and it always seems to be only a portion of the code, and you never explain which file we are looking at, or what is left out. I don't know if you have three files or ten files.

 

See attachment...

codeblock.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...