I have a form with textarea and dropzone for uploading images, and submit button. When the submit button is clicked - new comment is created and textarea content is saved to database, with all images added to dropzone (they are stored on server and in the database).
I'm trying to load div that contains comments via ajax, and below every comment there should be displayed all images for that particular comment.
I'm calling function like this:
<div id="comments_ticket_div"><?php echo commentsTicketList($id); ?></div>
And this is my function with mysql queries and output:
function commentsTicketList($id){
global $dbh1;
$query = $dbh1->query("SELECT a.*, b.avatar, b.first_name, b.last_name FROM `tickets_comments` AS a
INNER JOIN users AS b
ON a.tickets_comments_user = b.user_id
WHERE tickets_comments_parent = $id AND comments_parent=0 ORDER BY a.tickets_comments_date DESC");
$data = "";
while ($a_row = mysqli_fetch_array( $query )) {
$tickets_comments_id = $a_row['tickets_comments_id'];
$data .= '<span class="timeline-seperator text-center"> <span>'.$a_row['tickets_comments_date'].'</span></span>
<div class="btn-group pull-right"></div></span>
<div class="chat-body no-padding profile-message">
<ul>
<li class="message">
';
if (!empty ($a_row['avatar'])){
$data .= '<img src="'.ASSETS_URL.'/img/avatars/'.$a_row['avatar'].'-50.png" alt="User" class="" />
';
} else {
$data .= '<img src="'.ASSETS_URL.'/img/avatars/sunny.png" alt="User" class="" />';
}
$data .= '
<span class="message-text"> '.$a_row['first_name'].' '.$a_row['last_name'].' <!--small class="text-muted pull-right ultra-light"> 2 Minutes ago </small-->
'.$a_row['tickets_comments_body'].'
<div id="refresh-gallery">';
$queryx = $dbh1->query("SELECT img_url FROM `tickets_attachments` WHERE parent_comment_id = $tickets_comments_id AND parent_ticket_id = $id");
while ($b_row = mysqli_fetch_array( $queryx )) {
$data .= '<p>'.$b_row['img_url'].'</p>';
}
$data .= ' </div>
<ul class="list-inline font-xs">
<!--li>
<i class="fa fa-reply"></i> Odgovori
</li-->
<!--li>
ObriĊĦi
</li-->
</span>
</ul>
<!--input id="replubtn" name="replubtn" class="form-control input-xs" placeholder="Type and enter" type="text"-->
</li>
</ul>
</div>';
}
return $data;}
On success I'm loading that div with comments:
$("#comments_ticket_div").load( "<?php echo $_SERVER['REQUEST_URI'];?> #comments_ticket_div" );
And also div with query to get all img's for that comment:
$('#refresh-gallery').load("/.../.../.../edit-ticket2.php?id=<?php echo $id;?> #refresh-gallery");
As a result of this code, loading is working properly for all comments and image names are shown correctly (correct number of images and their names), but not for the last comment. The last comment is showing ALL images, but it should display only those images which belong to that last added comment (as the queryx is indicating above).
This has been solved...in the example given above I was loading #comments_ticket_div, and than later in code I was trying to load #refresh-gallery div that was inside of #comments_ticket_div - and that somehow created a problem.
For the solution, I removed loading of #refresh-gallery div, and apllied loading of #comments_ticket_div in Dropzone .on success function.
myDropzone.on("success", function (file) {
$('#comments_ticket_div').load("/../../../edit-ticket2.php?id=<?php echo $id;?> #comments_ticket_div");
});
Related
I am creating dynamic posts data on ajax call in which i am creating checkboxes also. but checkbox is not clickable. it is being showed on document but not responding on click. no showing tick i mean checked and uncheked states on user clicks. Everything is working fine except checkboxes. I did not done any jquery for checkboxes yet.
In Console i checked checkbox html and id value etc shown properly
here is my code //
if(mysqli_num_rows($result) > 0){
$output = "";
$output .= "<div class='col-12'><p id='content-title'>Fresh Posts</p></div>";
while($row = mysqli_fetch_assoc($result)) {
$title = substr($row['title'],0,25) . '...';
$description = substr($row['description'],0,200) . '...';
$output .= "<div class='col-4'>
<div class='post-content'>
<a href='single-post.php?id={$row['post_id']}' class='post-img'>
<img src='dashboard/uploads/{$row['post_img']}'>";
if(isset($_SESSION['logged-in'])){
$output .= "<input type='checkbox' id='{$row["post_id"]}' value='{$row['post_id']}' class='fav-icon-checkbox'>
<label for='{$row["post_id"]}'><i class='fa fa-heart fav-icon'></i></label>";
}
$output .= "</a>
<div class='post-info-container'>
<div class='post-info'>
<span class='post-author'><a href='#'>{$row['author']}</a></span>
<span class='dot'></span>
<span class='post-category'><a href='category.php?id={$row['category_id']}'>{$row['category_name']}</a></span>
<br>
<span class='post-date'>{$row['date']}</span>
</div>
<h5 class='post-title'>{$title}</h5>
<p class='post-description'>{$description}<a href='single-post.php?id={$row['post_id']}' class='read-more'>read more</a></p>
</div>
</div>
</div>";
}
echo $output;
}
Check the value attribute of the checkbox try changing the single quote :: $row["post_id"].
Im trying to Hide a comments that are not releated with the post above, the problem is that I have a database with all comments, and they are present in every post that I add... I'm trying to add a data-ID for every comments and trying to create a PHP "if" inside a generator post that if doesn't match doesn't show, but I think that i'm going to complicate myself, please help me:
this is a PHP function that create a comment and as you can see I added a data-POSTER that rappresent the ID of the post:
function createCommentRow($data,$utenteloggato) {
global $conn;
if ($utenteloggato == $data['userID']) {
$response = '
<div class="comment" data-postER="'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" >'.$data['comment'].'</div>
<div class="reply">REPLY</div>
<div class="replies">
<a id="option1"
data-id="'.$data['id'].'"
data-option="'.$data['tipo'].'"
href="javascript:void(0)"
onclick="goDoSomething(this);">
Delete
</a> ' ;
}
else
{
$response = '
<div class="comment" data-postER"'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>
<div class="reply">REPLY</div>
<div class="replies">
' ;
}
$sql = $conn->query("SELECT replies.id, name, comment, tipo, DATE_FORMAT(replies.createdOn, '%e/%c/%Y %T') AS createdOn, userID, postID FROM replies INNER JOIN users ON replies.userID = users.id WHERE replies.commentID = '".$data['id']."' ORDER BY replies.id DESC LIMIT 1");
while($data = $sql->fetch_assoc())
$response .= createCommentRow($data,$utenteloggato);
$response .= '
</div>
</div>
';
return $response;
}
inside ad another php I show all the post from database table "post", everything work fine, but inside this div where the comments go to display:
<div class="userComments" data-postID="'.$data['id'].'" > </div>
I want put a IF condition that if the value of data-postER from the comments doesn't much with the data-postID from class="userComments" it doesn't show up. Thanks to everybody
Since you are creating a big piece of html inside the $response...
How about, adding (just for the example I will use line by line) few or one line at a time with an if when coming in the line of userComment so for example:
else
{
$response = '
<div class="comment" data-postER"'.$data['postID'].'" >
<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>
<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>
<div class="reply">REPLY</div>
<div class="replies">
' ;
}
this becomes this:
else {
$response .= '<div class="comment" data-postER"'.$data['postID'].'" >';
$response .= '<div class="user">'.$data['name'].' <span class="time">'.$data['createdOn'].'</span></div>';
if($data['postID'] == $data['id']){
$response .= '<div class="userComment" data-postID="'.$data['postID'].'">'.$data['comment'].'</div>';
}
$response .= '<div class="reply">REPLY</div>';
$response .= '<div class="replies">';
}
This and now you either created the userComments or didn't...
You can ofcourse insert more than one line at a time but I wanted to illustrate it...
Also I would recommend not building the whole html inside a variable... rather building the html and inserting variables into it like this:
?><div class="comment" data-postER="<?php echo $data['postID'];?>" >
This way you embed php variables inside a healthy lookin html ...:)
So I am trying to build a simple search function for my website, and I don't want to have to refresh the page to return results. The code that i have here works perfectly, But I don't know how to Implement this using Jquery. I mainly have 2 pages, index.php and library.php
The section from the library.php that handles the search is as follows.
require_once('auth/includes/connect.php');
class MainLib
{
public function search($keyword){
try {
$db = DB();
$query = $db->prepare("SELECT houses.*, users.user_id FROM houses JOIN users ON users.user_id=houses.ownerid WHERE houses.location=:keyword");
$query->bindParam(":keyword", $keyword, PDO::PARAM_STR);
$query->execute();
if ($query->rowCount() > 0) {
return $query->fetchAll(PDO::FETCH_OBJ);
}
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
And the Section From the index.php that prints the results is as follows
<?php $hdata = new MainLib();
if(isset($_POST[$search])){
$houses = $hdata->search($search); // get user details
foreach($houses as $house){
?>
<div class="single-property-box">
<div class="property-item">
<a class="property-img" href="#"><img src="houses/<?php echo $house->main_image ?>" alt="#">
</a>
<ul class="feature_text">
<?php
if($house->featured=='true'){
?>
<li class="feature_cb"><span> Featured</span></li>
<?php }?>
<li class="feature_or"><span><?php echo $house->gender ?></span></li>
</ul>
<div class="property-author-wrap">
<a href="#" class="property-author">
<img src="dash/auth/users/<?php echo $house->profilepic ?>" alt="...">
<span><?php echo $house->title ?>. <?php echo $house->surname ?></span>
</a>
<ul class="save-btn">
<li data-toggle="tooltip" data-placement="top" title="" data-original-title="Bookmark"><i class="lnr lnr-heart"></i></li>
<li data-toggle="tooltip" data-placement="top" title="" data-original-title="Add to Compare"><i class="fas fa-arrows-alt-h"></i></li>
</ul>
</div>
</div>
<?php }}?>
So How would I accomplish the same result without having to reload the page every time ?
Here's a basic ajax search form setup using jQuery. Note that the search input is in a form with onsubmit="return false" - this is one way to prevent the default form submission which would trigger a page reload.
The button calls a function which gets the value of the search input, makes sure it's not blank, and puts it into an ajax function
The PHP page will receive the search term as $_GET['term'] and do it's thing. In the end, you will output (echo) html from your PHP functions, which the ajax done() callback will put into your search_results div. There are more optimized ways of transferring the data back - maybe a minimal json string that contains just the info you need for results, and your javascript takes it and inflates it into an html template. For large search results, this would be better because it means less data getting transferred and faster results.
function search() {
let term = $('#search_term').val().trim()
if (term == "") return; // nothign to search
$.ajax({
url: "searchFunctionPage.php",
method: 'get',
data: {
term: term
}
}).done(function(html) {
$('#search_results').html(html);
});
// sample data since we dont have the ajax running in the snippet
let html = 'Found 100 results.. <div>result 1</div><div>...</div>';
$('#search_results').html(html);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form onsubmit='return false;'>
<input type='text' placeholder="Type in your search keyphrase" id='search_term' />
<button type='button' onclick='search()'>Search</button>
</form>
<div id='search_results'></div>
I have successfully execute a load more button functionality using
jQuery - AJAX load() Method, it works like this , display first 3 comments from my db when the page is load , if user click load more button it will load 3 more comments , user click once load 3 more comments until there is no more comments from db.
The problem is when there is no more comments from db , user has to scroll all the way up. which is not very user-friendly, I know can make back to top link to guide user go back , are there any alternative solutions ?
like detect the last click or when there is no more data from db comments , then show less (display only first 3 comments), just like facebook style.
here is my codes
<script>
$(document).ready(function(){
<?php if ( more_than_3_reviews($the_post_id) ): ?>
var reviews_count = 3 ;
$("#show_more_reviews").click(function(){
reviews_count = reviews_count + 3;
$("#show_more").load("ajax/load_comments.php", {
new_comments_count : comments_count,
post_id: <?php echo $the_pro_id; ?>
});
});
<?php else: ?>
$("#show_more").remove();
<?php endif; ?>
});
</script>
my backend php file
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['new_comments_count']) and isset($_POST['post_id']) ){
$comments_count = $_POST['new_comments_count'];
$the_pro_id = $_POST['post_id'];
$approved = "approved";
$stmt10 = mysqli_prepare($connection,"SELECT comment_name, product_id,comment_msg, date_time FROM comments WHERE product_id = ? and comment_status = ? LIMIT $comments_count");
mysqli_stmt_bind_param($stmt10, "is", $the_pro_id , $approved );
mysqli_stmt_execute($stmt10);
mysqli_stmt_bind_result($stmt10, $comment_name,$product_id,$comment_msg,$date_time);
mysqli_stmt_store_result($stmt10);
while(mysqli_stmt_fetch($stmt10)){
$get_comment = <<<DELIMETER
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading"> {$comment_name} <small>{$date_time}</small> </h4>
{$comment_msg}
</div>
</div>
DELIMETER;
echo $get_comment;
}
} // post get comments
} // if its POST request
any assistance would be appreciated .
I want to display images in from server. In the database you can find the description and name of the image. the images should be displayed in a div where always the newest is on top. As the number of images increase so does the number of div's. so, the oldest will be at the bottom. I have read a number of posts and forums but I was unable to get the logic and the code on how to do it. I hope you can help me with my problem. Thanks a lot for your answer.
the only code i have is the formatting of the dive's:
<div class="gallery">
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
</div>
it's something like this:
newest image | some details here
newer image | some datails here
new image | some details here
old image | some details here
EDIT 1
I combined Lauri Elias' and iamde_coder's answer. I come up with this code which works almost similar to what is wanted. the only problem is that it displays the item (image&details in a div) 4 times. How can I eliminate the three? thanks!
$image_query = mysql_query ("SELECT filename, story FROM tbl_contest ORDER BY time DESC");
while($image_data = mysql_fetch_array($image_query)){
$imageName = stripslashes(mysql_real_escape_string($image_data['filename']));
$imageDetails = stripslashes(mysql_real_escape_string($image_data['story']));
$count = 0;
foreach($image_data as $imageName) {
echo '<div class="group">';
echo '<div class="images"><img src="/Mainfolder/image_entry/'.$imageName.'"></img></div>';
echo '<div class="details">'.$imageDetails.'</div></div>';
$count ++;
}
}
Now that you've given a little more code to help try using this:
$count = 0;
$image_query = mysql_query ("SELECT filename, story FROM tbl_contest ORDER BY time DESC") or die(mysql_error());
while($image_data = mysql_fetch_array($image_query)){
$imageName = stripslashes(mysql_real_escape_string($image_data['filename']));
$imageDetails = stripslashes(mysql_real_escape_string($image_data['story']));
$count++;
echo '<div class="group">';
echo '<div class="images"><img src="/Mainfolder/image_entry/'.$imageName.'" alt="Image '.$count.'" /></div>';
echo '<div class="details">'.$imageDetails.'</div></div>';
}
You could, for example, use a MySQL select somewhat like this:
SELECT name, description FROM my_database.images ORDER BY created_at DESC;
Then bind the result set to a variable in PHP like $images and then iterate on it and generate HTML like this:
foreach($images as $image) {
echo '<div class="group">';
echo '<div class="images"><img src="/images_folder/'.$image['name'].'"></img></div>';
echo '<div class="details">'.$image['description'].'</div></div>';
}