tags:

views:

91

answers:

4

I want to mess with jQuery and Ajax for a little while.

For my first I want to create just a single input box, type in my comment and then the comment will appear below. No page refreshes. How can I do this?

A: 

You should post the input box content to the server and append it to the comments upon getting success message from the server.

Giorgi
Okay I understood and looking over the guide from jQuery. Can I have a more code example though?
omnix
@kawoki see my comment below for some code :-)
Chris
+1  A: 

The basic idea is that you would have a client side page with a textarea or input. Attach a .click to a submit button and the .click will make a call to a server side script via .ajax.

Client Side:

<script type="text/javascript">
    $(document).ready(
    $('#submit').click({
       $.ajax({
                 type=POST,
                 data: "comment="+$("#comments").val(),
                 dataType: json,
                 url: 'somePage.php',
                 success: function(data) {
                     if(data.error){
                         alert("server reported error"); 
                     }else{

                         $('#postedComments').append(data);
                     }
                 }
               });


    });


});
</script>
<div id="postedComments></div>
<textarea id="comments"></textarea>
<input type="submit" id="submit" value="Post Comment" />

Server Side:

<?php
   if(isset($_POST['comments'])){
        //perform Database insert of value $_POST['comments']
        if(<database error>){
            echo json_encode(array('error'=>'-1'));
        }else{
            echo json_encode(array('success'=>'1'));
        }
   }
?>

Basically when client clicks submit, the .ajax posts "comments" to the server side script. The script then procseses request and reports back in json encoding error or success which is how in the success of ajax call you can determine what happened with the server call. Success in the ajax is not success it merely indicates the server responded back, which is why you can encode some message to send back to client script to determine if the server responded with error or success message.

Chris
Thanks! Ill use this if ----There isn't a only Ajax/Jquery way? I haven't got to learning PHP yet so i'm trying to avoid using PHP.
omnix
You can do this solely with jquery however your data will not persist if you close the browser and start a new session. Additionally, when client A goes to this site and posts comments client B will not see client A's comments and client Bs comments will not be seen by client A. Therefore you need some way for the data to persist which is why I present server side code.For merely the jquery client side, it would be the same functionality as posted without making the ajax call. Simple do $("#submit").click(function(){$("#postedComments").append($("#comments").val());});
Chris
Okay thank you chris, you was a big help!
omnix
Chris. This will be my last question > ive been reading a ebook about PHP but never attempt to do anything first. Where would I insert the PHP? index.html or another file.php?
omnix
The page you would insert into the place your ajax calls to. In my example i specify in the .ajax a url of somePage.php. (Also, you should accept people's answers if you like them. :-) Just click the checkmark under the answer you accept. Cheers and feel free to ask additional questions, I can edit my original post to help you out.
Chris
Done. Sorry I never noticed that!So I just insert the PHP into where the comments will be? and then that's done?
omnix
A: 

I have built a jQuery powered ajax comment system similar to that of wordpress. The create/delete actions are all ajax powered.

I done it by manually doing $.post() operations for creating comments and $.get() for deleting comments.

Paul Dragoonis