<?php
class Comments
{
public static function getComments()
{
}
public static function insert($commentText, $userId) //although insert to db but also return a stdClass Object from the database
{
$std = new stdClass(); //stdClass is a empty class that is already in PHP system
$std->comment_id = null; //name from variable in db
$std->comment = $comment_txt;
$std->userId = (int)$userId;
return $std;
}
public static function update($data)
{
}
public static function delete($commentId)
{
//delete the comment from the comment database using the id of comment_id
}
}
?>
อีกหน้าครับเอามาเผื่ออาจจะเกี่ยวข้อง
Code (JavaScript)
$(document).ready(function(){
$('#post-btn').click(function(){
comment_post_btn_click(); //เรียกใช้ function
});
});
function comment_post_btn_click()
{
var text = $('#posttext').val(); //get value from text
var userid = $('#user-id').val();
var user_name = $('#user-name').val();
if(text.length > 0 && userid != null)
{
$('.comment-insert-container').css('border', '1px solid #e1e1e1'); //after border red from emptypost so then if notempty post oldcolor will be back
$.ajax({
type: "POST",
url: "ajax/comment_insert.php",
data: {
task : "comment_insert",
userId : "userid",
comment : text
},
success : function(data)
{
comment_insert( jQuery.parseJSON(data));
console.log("ResponseText " + data);
},
//dataType: 'html' //not neccessary
});
console.log(text + " Username: " + user_name + " User ID: " + userid );
}
else
{
//text empty and then let's put a border of red on it
$('.comment-insert-container').css('border', '1px solid red');
console.log("The text was empty");
}
$('#posttext').val("");
}
function comment_insert(data)
{
var t = '';
t += '<li class="comment-holder" id="_'+data.comment_id+'">'; //copy from foler include/commentbox.php
t += '<div class="user-img">';
t += '<img src="'+data.profile_img+'" class="user-img-pic" />';
t += '</div> ';
t += '<div class="comment-body">';
t += '<h3 class="username-field">'+data.userName+'</h3>';
t += '<div class="comment-text">'+data.comment+'</div>';
t += '</div>';
t += '<div class="comment-button-holder">';
t += '<ul>';
t += '<li class="delete-btn">X</li>';
t += '</ul>';
t += '</div>';
t += '</li>';
$('.comment-holder-ul').prepend( t );
}