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.