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.
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">Follow Question 1</a>
</div>
<div>
questions 2
</div>
<div>
<a class="follow" href="http://example.com/follow/question/2">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'
);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.