Redisplay comments after posting one

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mephistoo
Forum Newbie
Posts: 6
Joined: Sat Jul 19, 2008 6:56 pm

Redisplay comments after posting one

Post by mephistoo »

Hi. I have a small comment box displayed on my page and the button points to a PHP script that simply modifies my file that stores all comments. I would like to now redirect the user back to the same page he was in before so that the whole thing looks instant. It should look like the button just posts it into the commentbox and thats it.
how can i do this? I tried doing a header() call at the end of the php script that points to the original page but that doesnt really work.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Redisplay comments after posting one

Post by shiznatix »

There are a few ways to do this. What I would do is use AJAX.

Have it so when your form submits, it really just calls a javascript function that passes the comment to an ajax call. Then have another javascript function that will get a list of the comments from the server via another ajax call and update the layout accordingly. The basic idea would be like this:

Code: Select all

 
//this function is called when you hit submit
function addComment()
{
    //comment_field is whatever you give as the id of the comment field
    var comment = document.getElementById('comment_field').value;
 
    //now just ajax to pass this variable "comment" to php. lets call that php file, add_comment.php
}
 

Code: Select all

 
//add_comment.php
if (isset($_POST['comment']))
{
    //you probably want to do some filtering here like strip_tags or whatever
    $comment = $_POST['comment'];
 
    //add $comment to the file/database
}
 
then just do the same idea with getting comments via another php file and using javascript to change the value of the <div> or whatever the comments are being held in. If you still have questions just ask.
mephistoo
Forum Newbie
Posts: 6
Joined: Sat Jul 19, 2008 6:56 pm

Re: Redisplay comments after posting one

Post by mephistoo »

thank you for response but i am really confused :( I got the part working where I just make HTTPrequest to the file and just paste that into innerHTML of my div.
But the part where I have to write to a file too is causing me a lot of trouble.

in my HTML page that includes the .js file with newComment() method:

Code: Select all

<form>
<p><label for="name">Name</label> <input type="text" name="name" /></p>
<p><label for="comment">Comment</label> <input type="text" name="comment" /></p>
<p class="submit"><input type="submit" value="Submit" onClick="newComment(cross)"/></p>
</form>
So here I am trying to tell the js file that the "cross" comment section is to be updated with Name & Comment from the form. Then:

Code: Select all

 
function newComment(where){
    var loc = "phpdb/"+where+"_comments.txt";
    xmlhttp=getXMLObject();
    xmlhttp.onreadystatechange=state_Change_Read;
    xmlhttp.open("GET","comments.php?loc="+loc,true);
    xmlhttp.send(null);
}
function state_Change_Read()
{
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {
    document.getElementById('comments').innerHTML=xmlhttp.responseText;
    }
}
}
 
So here I try to pass my comments.php the location of the file it should modify. I'll try to fetch it using $GET['loc']
But... how does the PHP file know what the Name and Comment is? The form has no action to it... and if i add the action, things will screw up. Right? eh :(
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Redisplay comments after posting one

Post by shiznatix »

ok you are on the right track. First, you want to change the way you are making your ajax call. Right now you are using GET. You want POST. More info on that can be found here: http://www.openjs.com/articles/ajax_xml ... g_post.php

Second, you need to pass the name and comment with that. The first part of this is to give your text fields an id. Like this:

Code: Select all

 
<p><label for="name">Name</label> <input type="text" name="name" id="name" /></p>
<p><label for="comment">Comment</label> <input type="text" name="comment" id="comment" /></p>
 
Next, In the previous link you will see
var params = "lorem=ipsum&name=binny"
what you want to do is something like:

Code: Select all

 
var name = document.getElementById('name').value;
var comment = document.getElementById('comment').value;
var params = "name="+name+"&comment="+comment;
 
you use javascript to get the values, then you will POST them to the comments file that will do the work for you. Try for a bit and if you get stuck then post what you have and ill give you some more guidance but not for at least 7 hours, I need some sleep. Good luck.
mephistoo
Forum Newbie
Posts: 6
Joined: Sat Jul 19, 2008 6:56 pm

Re: Redisplay comments after posting one

Post by mephistoo »

Thank you very much for your help! I think my code is working excellent now, but I am running into another issue which I was unable to fix:

It seems like my AJAX calls returns the exact same thing no matter how I modify the php file. Do the calls get cached somehow? That's pretty annoying. I use Firefox, and when I clear the cache I get different behavior.... this is so weird
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: Redisplay comments after posting one

Post by manixrock »

you are making a GET request, which is meant for retrieving information not changing it, where you pass parameters in the address and retrieve them in php trough $_GET or $_REQUEST (the latter is not recommended).

Code: Select all

...
   xmlhttp.open("GET","comments.php?loc="+loc,true);
   xmlhttp.send(null);
...
you should use POST in this case as it is meant for actions like posting a comment. In php you retrieve them just like GET, except you use $_POST (you can also use $_REQUEST which gets both GETs and POSTs but it's not recommended).

Code: Select all

...
   xmlhttp.open("POST","comments.php",true);
   xmlhttp.send("loc="+loc);
...
mephistoo
Forum Newbie
Posts: 6
Joined: Sat Jul 19, 2008 6:56 pm

Re: Redisplay comments after posting one

Post by mephistoo »

no i think i use POST now, as shiznatix recommended

EDIT: I solved it by using the jQuery library to do my AJAX calls. I guess my Javascript code was wrong somewhere. ah well
Post Reply