views:

138

answers:

1

I am building a web application that will let users follow discussion threads, in Q&A format. To that end, when displaying questions, I have a "Follow" button next to each question. I want users to be able to follow these threads without reloading the page, thus using AJAX. So, I want the AJAX call to:

1) Submit a form updating the database recording following relationships; and 2) Replace "Follow" submit button with a "Following"

Here's my JavaScript code:

$("#submitFollow").click(function() {
    var type = $("input#type").val();
    var u_id = $("input#u_id").val();
    var e_id = $("input#e_id").val();
    var dataString = 'type='+ type + '&u_id=' + u_id + '&e_id=' + e_id;

    $.ajax({
        type: "POST",
        url: "<?=site_url()?>/follow/ajaxFollow",
        data: dataString,
        success: function() {
            $('#following').html("Following");
            .hide()
            .fadeIn(1500)
        }
    });
    return false;
});
});

Here is what my form code looks like. I stripped out the action and method to keep the form from submitting itself regularly. I tried to preventDefault using JS, but that didn't work. (The values are filled in by the model):

<div id="following">
<form id="followForm" action="" method="">
<input type="hidden" name="type" id="type" value="question">
<input type="hidden" name="u_id" id="u_id" value="1">
<input type="hidden" value="12" name="e_id" id="e_id">
<input type="submit" id="submitFollow" value="Follow Question" class="submitFollow">
</form>
</div>

Right now the controller is pretty straightforward -- it just takes the array of post variables and submits them straight to the database.

I'm not too great at JavaScript, so my apologies if I'm taking up your time with a straightforward problem.

+1  A: 

I'll just be general here, but hopefully it will help. If it were me, I would do something like the following:

Here's some html, generated from codeigniter:

<div>
questions 1
</div>
<div>
<a class="follow" href="http://example.com/follow/question/1"&gt;Follow Question 1</a>
</div>

<div>
questions 2
</div>
<div>
<a class="follow" href="http://example.com/follow/question/2"&gt;Follow Question 2</a>
</div>

Then make everything work without javascript (you add that later). So here's the codeigniter controller with some general code:

 <?php

class Follow extends Controller {

    function Follow()
    {
        parent::Controller();
        $this->auth->restrict_to_admin();
    }

    function question($id = NULL)
    {
        if($id)
        {
            //get question from database using the id
            //i assume somehow the user is logged in? use their id where applicable
            echo '<span>You are now following this question</span>';
        }
    }

}

/* End of file follow.php */

Now, whether they're using javascript or not, it will work. Here's where you add the javascript (I'm assuming you're using jquery?). If not, it will be close enough.

$(document).ready(function(){
    $('follow').click(function(event){
        event.preventDefault();

        var followUrl = $(this).attr('href');
        //do ajax call here.
        $.post(
        followUrl,
        {
            //any paramaters you need to add can
            //go here in this format:
            //param_key: param_value
        },
        function(responseText){
            //here you can update the clicked link to say something
            //like "you are now following this question"
            //the responseText var will have whatever text the server responds with.
            //just responsd with html
        },
        'html'
        );

    });
});
Matthew
I like this solution a lot, but I can't get event.preventDefault() to do anything for my site. The record is added to the database just fine, but the link works, no Ajax. I have jquery installed and have used it for other things just fine, but I can't stop the default activity for any forms or links. Putting in a return false; doesn't do anything either. Any suggestions?
tchaymore
Well, I'm not sure what actually did it, but I finally got it to work. I replaced event.preventDefault() with return false and moved it to the end of the function. So far, so good.
tchaymore