I am working on a commenting system and I was able to get it working by using 5 separate scripts, which are exactly the same, except for having slightly different variables. I had to repeat (copy & paste) the js function 5 times in the html <head>, and only allow for 5 posts per page. Which works fine. But, I thought maybe I could replace it with a single function that includes an iteration counter that changes the variables automatically?
Here's what I have now (below), if you think this can be easily turned into 1 function with iteration counter, please let me know how, as I am new to js. I already have the iteration working on the php end, but, I just couldn't figure out how to do it for the javascript.
<script type="text/javascript">
$(function() {
$(".submit1").click(function() {
var post_id = $("#post_id1").val();
var member_id = $("#member_id").val();
var comment = $("#comment1").val();
var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment;
if(comment=='') {
alert('Please enter a valid comment 1');
} else {
$("#flash1").show();
$("#flash1").fadeIn(2400).html('<span class="loading">Loading Comment...</span>');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("li#update1").append(html);
$("li#update1 li:last").fadeIn("3600");
$("#flash1").hide();
document.getElementById('post_id1').value='';
document.getElementById('member_id').value='';
document.getElementById('comment1').value='';
$("#comment1").focus();
$("#flash1").hide();
}
});
}
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$(".submit2").click(function() {
var post_id = $("#post_id2").val();
var member_id = $("#member_id").val();
var comment = $("#comment2").val();
var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment;
if(comment=='') {
alert('Please enter a valid comment 2');
} else {
$("#flash2").show();
$("#flash2").fadeIn(2400).html('<span class="loading">Loading Comment...</span>');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("li#update2").append(html);
$("li#update2 li:last").fadeIn("3600");
$("#flash2").hide();
document.getElementById('post_id2').value='';
document.getElementById('member_id').value='';
document.getElementById('comment2').value='';
$("#comment2").focus();
$("#flash2").hide();
}
});
}
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$(".submit3").click(function() {
var post_id = $("#post_id3").val();
var member_id = $("#member_id").val();
var comment = $("#comment3").val();
var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment;
if(comment=='') {
alert('Please enter a valid comment 3');
} else {
$("#flash3").show();
$("#flash3").fadeIn(2400).html('<span class="loading">Loading Comment...</span>');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("li#update3").append(html);
$("li#update3 li:last").fadeIn("3600");
$("#flash3").hide();
document.getElementById('post_id3').value='';
document.getElementById('member_id').value='';
document.getElementById('comment3').value='';
$("#comment3").focus();
$("#flash3").hide();
}
});
}
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$(".submit4").click(function() {
var post_id = $("#post_id4").val();
var member_id = $("#member_id").val();
var comment = $("#comment4").val();
var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment;
if(comment=='') {
alert('Please enter a valid comment 4');
} else {
$("#flash4").show();
$("#flash4").fadeIn(2400).html('<span class="loading">Loading Comment...</span>');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("li#update4").append(html);
$("li#update4 li:last").fadeIn("3600");
$("#flash3").hide();
document.getElementById('post_id4').value='';
document.getElementById('member_id').value='';
document.getElementById('comment4').value='';
$("#comment4").focus();
$("#flash4").hide();
}
});
}
return false;
});
});
</script>
<script type="text/javascript">
$(function() {
$(".submit5").click(function() {
var post_id = $("#post_id5").val();
var member_id = $("#member_id").val();
var comment = $("#comment5").val();
var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment;
if(comment=='') {
alert('Please enter a valid comment 5');
} else {
$("#flash5").show();
$("#flash5").fadeIn(2400).html('<span class="loading">Loading Comment...</span>');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("li#update5").append(html);
$("li#update5 li:last").fadeIn("3600");
$("#flash5").hide();
document.getElementById('post_id5').value='';
document.getElementById('member_id').value='';
document.getElementById('comment5').value='';
$("#comment5").focus();
$("#flash5").hide();
}
});
}
return false;
});
});
</script>