davej Posted May 10, 2014 Share Posted May 10, 2014 I admit I do not know how browser caching is supposed to work with dynamic pages such as forums. There are schemes to avoid caching. Link to comment Share on other sites More sharing options...
etsted Posted May 10, 2014 Author Share Posted May 10, 2014 So i have tried to ise jQuery with my AJAX, but i still have to refresh the page in order for the comment system to work. I am being able to post comments, but when i try to reply to that comment i get errors saying that serten variables are not set function replyToStatus(sid,user,ta,btn,document) { $(document).ready(function(){ // sid holds the id that associate each thread with all of its replies // get the values inside the textarea var data = _(ta).value; if(data === ""){ alert("Type something first"); return false; } // select the buttons id _("replyBtn_"+sid).disabled = true; $.post("php_parsers/status_system2.php","action=status_reply&sid="+sid+"&user="+user+"&data="+data+"&document="+document,function(information){ // split the incoming string, to get the last inserted id var datArray = information.split("|"); if(datArray[0] === "reply_ok"){ // contains the last inserted id to the DB var rid = datArray[1]; // sanitize the data, the user wrote in data = data.replace(/</g,"<").replace(/>/g,">").replace(/n/g,"<br />").replace(/r/g,"<br />"); // contains the reply _("status_"+sid).innerHTML += '<div id="reply_'+rid+'" class="reply_boxes"><div><b>Reply by you just now:</b><span id="srdb_'+rid+'"><a href="#" onclick="return false;" onmousedown="deleteReply(''+rid+'','reply_'+rid+'');" title="DELETE THIS COMMENT">remove</a></span><br />'+data+'</div></div>'; _("replyBtn_"+sid).disabled = false; // empty the textarea _(ta).value = ""; } else { alert(information); } }); });}; this function is used to reply a comment on an already existing thread Link to comment Share on other sites More sharing options...
davej Posted May 10, 2014 Share Posted May 10, 2014 Ok, so why are certain variables not set? What are you doing for initialization after every page-load? Do you create a session? Link to comment Share on other sites More sharing options...
etsted Posted May 10, 2014 Author Share Posted May 10, 2014 They get set when a user fills up a textarea, and hits a button. $statuslist .= '<textarea id="replytext_'.$statusid.'" class="replytext" onkeyup="statusMax(this,250)" placeholder="write a comment here"></textarea><button id="replyBtn_'.$statusid.'" onclick="replyToStatus('.$statusid.',''.$u.'','replytext_'.$statusid.'',this,''.$DB_table.'')">Reply</button>'; Link to comment Share on other sites More sharing options...
etsted Posted May 10, 2014 Author Share Posted May 10, 2014 <textarea id="replytext_8" class="replytext" onkeyup="statusMax(this,250)" placeholder="write a comment here"></textarea><button id="replyBtn_8" onclick="replyToStatus(8,'george','replytext_8',this,'comments')">Reply</button> If you need to know, the content of the reply textarea looks like this, but i think i have a cache problem Link to comment Share on other sites More sharing options...
davej Posted May 10, 2014 Share Posted May 10, 2014 You keep showing us little pieces of code but you don't explain how everything gets initialized. Even if the browser caches the page your initialization code should be able to detect and correct this situation almost immediately. Link to comment Share on other sites More sharing options...
etsted Posted May 10, 2014 Author Share Posted May 10, 2014 At top i have a variable that checks to see which of the page the user is on $check_document = $_SERVER['PHP_SELF']; if($check_document == "/watch.php") { $DB_table = "comments"; } else if($check_document == "/user.php") { $DB_table = "status"; } this is the reply textarea $statuslist .= '<textarea id="replytext_'.$statusid.'" class="replytext" onkeyup="statusMax(this,250)" placeholder="write a comment here"></textarea><button id="replyBtn_'.$statusid.'" onclick="replyToStatus('.$statusid.',''.$u.'','replytext_'.$statusid.'',this,''.$DB_table.'')">Reply</button>'; this is the javascript that is supposed to handle the input the user just typped in, then send it of to php_parsers/status_system2.php function replyToStatus(sid,user,ta,btn,document) { $(document).ready(function(){ // sid holds the id that associate each thread with all of its replies // get the values inside the textarea var data = _(ta).value; if(data === ""){ alert("Type something first"); return false; } // select the buttons id _("replyBtn_"+sid).disabled = true; $.post("php_parsers/status_system2.php","action=status_reply&sid="+sid+"&user="+user+"&data="+data+"&document="+document,function(information){ // split the incoming string, to get the last inserted id var datArray = information.split("|"); if(datArray[0] === "reply_ok"){ // contains the last inserted id to the DB var rid = datArray[1]; // sanitize the data, the user wrote in data = data.replace(/</g,"<").replace(/>/g,">").replace(/n/g,"<br />").replace(/r/g,"<br />"); // contains the reply _("status_"+sid).innerHTML += '<div id="reply_'+rid+'" class="reply_boxes"><div><b>Reply by you just now:</b><span id="srdb_'+rid+'"><a href="#" onclick="return false;" onmousedown="deleteReply(''+rid+'','reply_'+rid+'');" title="DELETE THIS COMMENT">remove</a></span><br />'+data+'</div></div>'; _("replyBtn_"+sid).disabled = false; // empty the textarea _(ta).value = ""; } else { alert(information); } }); });}; this is the ajax that will hande the AJAX call //action=status_reply&sid="+sid+"&user="+user+"&data="+dataif (isset($_POST['action']) && $_POST['action'] == "status_reply" && !empty($_POST['document']) ){ // Make sure data is not empty if(strlen($_POST['data']) < 1){ echo "data_empty"; exit(); } // Clean the posted variables $osid = preg_replace('#[^0-9]#', '', $_POST['sid']); $account_name = preg_replace('#[^a-z0-9]#i', '', $_POST['user']); $data = htmlentities($_POST['data']); $data = mysqli_real_escape_string($con, $data); // Make sure account name exists (the profile being posted on) $sql = "SELECT COUNT(id) FROM register WHERE u_name='$account_name' AND activated='1' LIMIT 1"; $query = mysqli_query($con, $sql); $row = mysqli_fetch_row($query); if($row[0] < 1){ echo "the user doesnt exist"; exit(); } // check to see which page the user is on, then give different variables that contain different DB tables// check to see whether or not the user replied to a status from user.php or watch.php if($_POST['document'] == "comments") // this means the user replied within watch.php { $DB_table = "comments"; } else if($_POST['document'] == "status") // this mean that the user replied within user.php { $DB_table = "status"; } // Insert the status reply post into the database $sql = "INSERT INTO $DB_table(osid, account_name, author, type, data, postdate) VALUES('$osid','$account_name','$log_username','b','$data',now())"; $query = mysqli_query($con, $sql); // get the inserted id // to allows the writer or the account owner to delete the reply $id = mysqli_insert_id($con); // Insert notifications for everybody in the conversation except this author $sql = "SELECT author FROM $DB_table WHERE osid='$osid' AND author!='$log_username' GROUP BY author"; $query = mysqli_query($con, $sql); while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { $participant = $row["author"]; $app = "Status Reply"; $note = $log_username.' commented here:<br /><a href="user.php?u='.$account_name.'#status_'.$osid.'">Click here to view the conversation</a>'; mysqli_query($con, "INSERT INTO notifications(username, initiator, app, note, date_time) VALUES('$participant','$log_username','$app','$note',now())"); } echo "reply_ok|$id"; exit();} Link to comment Share on other sites More sharing options...
davej Posted May 10, 2014 Share Posted May 10, 2014 Your problem may be due to the fact that the Php code only runs when the page is rendered. If the page loads from the browser cache the Php code doesn't run. --EDIT-- Actually that doesn't make any sense because although the Php code doesn't run the cached page would consist of what the Php code had produced the last time it ran. Link to comment Share on other sites More sharing options...
etsted Posted May 10, 2014 Author Share Posted May 10, 2014 what do u suggest? Link to comment Share on other sites More sharing options...
davej Posted May 10, 2014 Share Posted May 10, 2014 Well, you need to go through your code with that thought in mind. Maybe your Javascript should wake up and look at a (Php generated) page version number and then ask the server what the current version number is. Also when Php receives a POST from Ajax have Php test the POST for validity and report any errors. Likewise when the Javascript receives the response from Php also test that the response was valid. In my problem case I have a slideshow that occasionally glitches as if it has (perhaps) loaded a cached version, so maybe I need to write code that runs immediately rather than waiting for window.onload. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 10, 2014 Share Posted May 10, 2014 (edited) What exactly are expecting from that code, cause all i see being returned is "data_empty", "the user doesnt exist" or echo "reply_ok| n"; where n equals the auto incremented id ref. From what i can see it is just inserting new comments in the background into the database, you would need to run another ajax call or just retrieve the new comments using select query depending on how you want this to work. What you should do is forget about ajax, and create a separate php page just to show required details, once the new php page is working you can basically use this to produce the same result for ajax process Edited May 11, 2014 by dsonesuk Link to comment Share on other sites More sharing options...
davej Posted May 10, 2014 Share Posted May 10, 2014 I'm not a jQuery guy but I don't think you ever, ever do this... function replyToStatus(sid,user,ta,btn,document) { $(document).ready(function(){ Link to comment Share on other sites More sharing options...
dsonesuk Posted May 11, 2014 Share Posted May 11, 2014 $(document).ready() is equivalent to window.onload, and only runs when page is fully loaded, you can't use it within a function, as the loading of page would have already been completed, so it won't be triggered. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now