Reputation: 131
I am making a commenting reply system in PHP jquery and Ajax. So far I managed to make the commenting and reply to comment action with php and jquery ajax. When I reply to a comment, i receive back from the jquery ajax the number of replies to a comment and i print the number on the screen. Now what i want to do next is printing the reply to the screen after it was submitted. I made the jquery ajax function and wrote my php script. Then I echo the $output formated from the php back to the ajax. The problem is i am receiving blank response nevertheless i tested the php file directly and it is working perfectly and outputting to the screen the $output variable. Please help. Here is my ajax part that takes care of the output replies :
<script type="text/javascript">
load_replies();
function load_replies() {
$.ajax({
url: "widgets/board_reply_fetch.php?comment_id=<?php echo
$board_comment_id_number;?>",
method: "POST",
success: function(data){
$("#reply_comment").html(data);
console.log(data);
}
});
}
</script>
and here is my php file :
<?php
require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';
if (isset($_GET["comment_id"])) {
$comment_id = (int)$_GET["comment_id"];
$reply_data = find_board_replies_by_comment_id($comment_id);
$output = "";
$output_array = array();
while ($reply_assoc = mysqli_fetch_assoc($reply_data)) {
$reply_comment_id = $reply_assoc['comment_id'];
$reply_board_id = $reply_assoc['board_id'];
$reply_user_id = $reply_assoc['user_id'];
$reply_text = $reply_assoc['reply'];
$reply_timestamp = $reply_assoc['reply_timestamp'];
$reply_user_data = find_user_data_by_id($reply_user_id);
$profile_image = $reply_user_data['profile_picture'];
$profile_image_thumb = "../uploaded_pictures/profile/$reply_user_id/" . $reply_user_id . "small.png";
if ($profile_image == "") {
if ($comment_user_data['gender'] == "Male"){
$user_profile_picture = "../images/ProfilePicMale.png";
} else {
$user_profile_picture = "../images/ProfilePicFemale.png";
}
} else {
$user_profile_picture = $profile_image_thumb;
}
$full_name = ucfirst(htmlentities($reply_user_data['first_name'])) . " " . ucfirst(htmlentities($reply_user_data['last_name']));
$time_of_post = time_of_post($reply_timestamp);
$the_reply_text = nl2br($reply_text);
$output = "<div class=\"reply_comment_div\">";
$output .= "<a href=\"profile.php?user_id=$reply_user_id\" class=\"board_comments_div_picture\">";
$output .= "<img src=\"$user_profile_picture\" width=\"50px\" height=\"50px\" /></a>";
$output .= "<a href=\"profile.php?user_id=$reply_user_id\" class=\"board_comments_reply_link\">$full_name</a>";
if ($reply_user_id == $_SESSION['user_id']){
$output .= "<a href=\"edit_comment_board.php?comment_id=$reply_comment_id\" class=\"edit_comment_button_board\">Edit</a>";
$output .= "<a href=\"widgets/delete_board_comment.php?board_id=$reply_board_id&comment_id=$reply_comment_id\" onclick=\"return confirm('Are you sure you want to delete this admin?')\" class=\"delete_button_status\">Delete</a>";
}
$output .= "<div class=\"board_comment_submited_on\">submitted $time_of_post</div>";
$output .= "<span class=\"comment_content_span\">$the_reply_text</span>";
$output .= "</div>";
$output_array[] = $output;
}
foreach ($output_array as $array) {
echo $array;
}
}
?>
Upvotes: 1
Views: 580
Reputation: 345
try to store you data that is being sent to php page in data object
example:
<script type="text/javascript">
load_replies();
function load_replies() {
var commment_id = "<?php echo $board_comment_id_number;?>";
$.ajax({
url: "widgets/board_reply_fetch.php",
data:{commentId:commment_id },//commment_id store it here
method: "GET",
success: function(data){
$("#reply_comment").html(data);
console.log(data);
}
});
}
</script>
Upvotes: 1
Reputation: 402
Use POST METHOD:
if (isset($_POST["comment_id"])) {
$comment_id = (int)$_POST["comment_id"];
Upvotes: 0