Jquery datepicker with Ajax is not working - php

In my website there are many categories. Each category page have their posts. Here I used jQuery datepicker, if user want to see Aug 20th posts, they click particular date on calender & see the date posts. One more thing, if I open one category the posts of today should be displayed only. Please check this code and help me. This code displays all posts of category & datepicker is not retrieving anything.
This is mysql query in function.php
if(isset($_REQUEST['datepost']))
{
$date = $_POST['date'];
$res=mysql_query("SELECT * FROM `wp_posts` WHERE DATE_FORMAT(post_date, '%m/%d/%Y' ) = '%m/%d/%Y' AND post_type = 'post' ORDER BY post_date DESC");
$pageposts=mysql_fetch_array($res);
exit();
}
?>
This is my script & PHP code:
<script type="text/javascript">
$(function() {
<!--current date posts-->
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var today_date = day + "/" + month + "/" + year;
var dataString ='date='+today_date;
$.ajax ({
type: "POST",
url: "<?php echo home_url(); ?>/?datepost",
data: dataString,
success: function(data) {
$('#testdiv').html(data);
}
}); <!--End current date posts-->
<!--select date posts-->
$("#datepicker").datepicker ({
onSelect: function(dateTypeVar, inst) {
var dateAsObject = $(this).val();
var dataString ='date='+dateAsObject;
$.ajax ({
type: "POST",
url: "<?php echo home_url(); ?>/?datepost",
data: dataString,
success: function(data) {
$('#testdiv').html(data);
}
}); <!--End select date posts-->
}
});
});
</script>
PHP coding:
Date: <input type="text" id="datepicker" size="30"/>
<?php while (have_posts()) :the_post();
?>
<div id="testdiv">
<div class="featuredpost">
<div class="cat-block">
<?php the_post_thumbnail( 'homepage-catpage' ) ?>
<h2 class="posttitle"> <a href="<?php echo the_permalink();?>" rel="bookmark" title="" ><?php echo $key_1_values = get_post_meta($postid, '_visual-subtitle', true ); ?></a>
</h2>
<p> <?php $content = get_the_excerpt();
$contentrecord=htmlspecialchars_decode(strip_tags(stripslashes($content)));
echo substr($contentrecord, 0, 350); ?></p>
<p class="postmeta"><span class="meta_date"><?php the_time('Y/m/d g:i:s A'); ?>
<input id="name" type="hidden" value="<?php echo $postid; ?>" name="post__id">
<p>
</span><span class="meta_permalink">التفاصيل </span> </p>
</div>
</div>
</div>
<?php endwhile;
wp_reset_query();?>
</div>

<?php if(isset($_REQUEST['datepost']))
{
$date = date('m/d/Y',strtotime($_POST['date']));
$res=mysql_query("SELECT * FROM `wp_posts` WHERE DATE_FORMAT(post_date, '%m/%d/%Y' ) = $date AND post_type = 'post' ORDER BY post_date DESC");
$pageposts=mysql_fetch_array($res);
exit();
}
?>
Provide actual value for post_date column like DATE_FORMAT(post_date, '%m/%d/%Y' ) = $date

Related

Why my Ajax/php request is executed twice?

well, i've an like/unlike script.
When i click on like or unlike, the php request is executed twice.
I've tried several things but i don't understand why.
Here my php request.
$query = "SELECT * FROM like_unlike WHERE postid=".$postid." and userid=".$userid;
$req = $bdd->prepare($query);
$req->execute();
$array = $req->fetchALL();
$count = count($array);
if($count == 0){
$insertquery = "INSERT INTO like_unlike(userid,postid,type)
values(".$userid.",".$postid.",".$type.")";
$insertquery = $bdd->prepare($insertquery);
$insertquery->execute();
and here my js file
$(document).ready(function(){
// like and unlike click
$(".like, .unlike").click(function(){
var id = this.id; // Getting Button id
var split_id = id.split("_");
var text = split_id[0];
var postid = split_id[1]; // postid
// Finding click type
var type = 0;
if(text == "like"){
type = 1;
}else{
type = 0;
}
// AJAX Request
$.ajax({
url: 'likeunlike.php',
type: 'post',
data: {postid:postid,type:type},
dataType: 'json',
success: function(data){
var likes = data['likes'];
var unlikes = data['unlikes'];
var difflikes = data['likes'] - data['unlikes'];
$("#likes_"+postid).text(likes); // setting likes
$("#unlikes_"+postid).text(unlikes);
$("#difflikes_"+postid).text(difflikes); // setting unlikes
}
});
});
HTML:
<img src="<?= $chemin ?>img/up.png" value="Like" id="<?php echo $id_vote_like; ?>"
class="img_vote_up zoom <?= $class_vote_like ?> " style="" /> <?= $log_e ?>
<span id="difflikes_<?php echo $postid; ?>" class="nb_votes"><?php echo $likes_d; ?>
</span>
<?= $log_i ?>
<img src="<?= $chemin ?>img/down.png" value="Unlike" id="<?php echo
$id_vote_unlike; ?>" class="<?= $class_vote_unlike ?> img_vote_down zoom" style=""
/> <?= $log_e ?>
Anyone has an idea ?

Ajax not loading posts on live server but working on localhost

I'm trying to load posts of users from my database to the website but the ajax part isn't loading for some reason. It was working on localhost but not working on the live server. Is there some problem in the ajax code or the code written in the index page?
Here in the index page the posts_area div part isn't loading
index.php
<?php
include("includes/header.php");
if(isset($_POST['post'])){
$post = new Post($con, $userLoggedIn);
$post->submitPost($_POST['post_text'], 'none');
}
?>
<div class="main_column column">
<form class="post_form" action="index.php" method="POST">
<textarea name="post_text" id="post_text" placeholder="Got something to say?"></textarea>
<input type="submit" name="post" id="post_button" value="Post">
<hr>
</form>
<div class="posts_area"></div>
<img id="loading" src="assets/images/icons/loading.gif">
</div>
<div class="user_details column">
<h4>Popular</h4>
<div class="trends">
<?php
$query = mysqli_query($con, "SELECT * FROM trends ORDER BY hits DESC LIMIT 9");
foreach ($query as $row) {
$word = $row['title'];
$word_dot = strlen($word) >= 14 ? "..." : "";
$trimmed_word = str_split($word, 14);
$trimmed_word = $trimmed_word[0];
echo "<div style'padding: 1px'>";
echo $trimmed_word . $word_dot;
echo "<br></div><br>";
}
?>
</div>
</div>
<script>
var userLoggedIn = '<?php echo $userLoggedIn; ?>';
$(document).ready(function() {
$('#loading').show();
//Original ajax request for loading first posts
$.ajax({
url: "https://bestconnect.000webhostapp.com/includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=1&userLoggedIn=" + userLoggedIn,
cache:false,
success: function(data) {
$('#loading').hide();
$('.posts_area').html(data);
}
});
$(window).scroll(function() {
var height = $('.posts_area').height(); //Div containing posts
var scroll_top = $(this).scrollTop();
var page = $('.posts_area').find('.nextPage').val();
var noMorePosts = $('.posts_area').find('.noMorePosts').val();
if ((document.body.scrollHeight == document.body.scrollTop + window.innerHeight) && noMorePosts == 'false') {
$('#loading').show();
var ajaxReq = $.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=" + page + "&userLoggedIn=" + userLoggedIn,
cache:false,
success: function(response) {
$('.posts_area').find('.nextPage').remove(); //Removes current .nextpage
$('.posts_area').find('.noMorePosts').remove(); //Removes current .nextpage
$('#loading').hide();
$('.posts_area').append(response);
}
});
} //End if
return false;
}); //End (window).scroll(function())
});
</script>
</div>
ajax_load_posts.php
<?php
include("../../config/config.php");
include("../classes/User.php");
include("../classes/Post.php");
$limit = 10; //Number of posts to be loaded per call
if (isset($_GET['posts'])) {
$posts = new Post($con, $_REQUEST['userLoggedIn']);
$posts->loadPostsFriends($_REQUEST, $limit);
}
?>
<?php
header("Access-Control-Allow-Origin: *");
include("../../config/config.php");
include("../classes/User.php");
include("../classes/Post.php");
$limit = 10; //Number of posts to be loaded per call
if (isset($_GET['userLoggedIn']))
{
$posts = new Post($con, $_REQUEST['userLoggedIn']);
$posts->loadPostsFriends($_REQUEST, $limit);
}
?>

PHP show more system

I have got this HTML
<div class="V_CPp">
<span class="ViewC">View more comments</span>
</div>
<div class="hov_c">
<div class="CommentsAwp Comment_Hs CA">
<img src="../users/<?php echo $CommenterPicture[0];?>">
<span><?php echo $CommenterName[0]; ?></span>
<span class="s2"><?php echo $CommentRow[1]; ?></span>
</div>
</div>
And this Jquery/AJAX
$(".V_CPp").click(function(event) {
var numItems = $(this).siblings('.F_W_comments').children().children().length;
var i=$(this).siblings(".c_jq").children(".Likes").attr("data-i");
$.ajax({
url: '../connect.php',
type: 'GET',
cache: false,
contentType: false,
processData: false,
data:"VMC="+i+"&NI="+numItems,
success: function(data)
{
$("body").html(data);
}
});
});
And this php(connect.php)
if (isset($_GET['VMC'])) {
$RandS=$_GET['VMC'];
$From=$_GET['NI'];
$To=$From+4;
$query=$con->query("SELECT id FROM uploads WHERE Rand='$RandS'");
$Id=$query->fetch_row();
$Commentsq=$con->query("SELECT * FROM (SELECT * FROM comments WHERE Post_id='$Id[0]' ORDER BY `DATE` DESC LIMIT $From,$To) AS sub ORDER BY `DATE` ASC");
while($COMM=$Commentsq->fetch_row())
{
echo $COMM[1]."<br />";
}
}
So when i have lets say 13 comments in order from 1 to 13
It only shows 10-13 and when i click
<div class="V_CPp">
It shows me all comments from 2 to 9 instead of 6-9
Did you try with
<span class="s2"><?php echo $CommentRow[0]; ?></span>
instead
<span class="s2"><?php echo $CommentRow[1]; ?></span>
Try and comment please, maybe this is the issue.
Best of lucks,

Append content to div via jquery

My project is a kind of blog, with posts and comments area. I'm trying to make it async with jquery ajax. My problem is that when a new comment gets inserted asynchronously it appears in the div twice, and all the other comments disappear. If i refresh the page it looks fine.
VIEW:
<section id="comments_section_<?php echo $p1['post_id'];?>"> //THIS IS THE OUTPUT WHERE NEW COMMENTS WILL BE APPENDED
<?php
$data['comments'] = $this->model_mutamba->getComments($p1['post_id']);
if($data['comments'] != ""){ ?>
<div class="comments_container" id="comments_container_<?php echo $p1['post_id'];?>">
<?php foreach ($data['comments'] as $c){
$user_img3 = $this->model_mutamba->getUserPicId($c->aluno_id);
?>
<div>
<img style="height:32px;" src="<?php echo site_url();?>img/users/portfolio/<?php echo $user_img3;?>" alt="" class="align-left" />
<p>
<?php echo $this->model_mutamba->addSmilies($c->comment);?>
<br>
<span class="comments_date">
<?php
$c_date = explode('-', substr($c->comment_date,0,-8));
$date_num_word = monthNumToWords($c_date[1]);
echo substr($c->comment_date, 11,-3)." ".$c_date[2]." de ". $date_num_word . " ". $c_date[0];?>
</span>
</p>
</div>
<?php } ?>
</div>
<?php } ?>
</section>
JS:
function comment(post_id,input){
var comment = $(input).val();
var output = '#comments_section_'+post_id;
if(comment != ""){
$.ajax({
type:"POST",
url:base_url+"mutamba/insertNewComment",
data: {comment: comment, post_id:post_id},
cache:false,
success:function(response){
$(output).html(response);
$(output).append(response);
}
});
}
}
CONTROLLER:
function insertNewComment(){
if($this->session->userdata('is_logged_in')){
$email= $this->session->userdata('email');
$comment= $this->FilterData($this->input->post('comment'));
$post_id= $this->FilterData($this->input->post('post_id'));
$this->load->model('model_mutamba');
if($this->model_mutamba->insertNewComment($email,$comment,$post_id)){
$this->load->model('model_user');
$count = $this->model_mutamba->countPostComments($post_id);
?>
<script>
var count = '<?php echo $count;?>';
var comments_count = '<?php echo "#comments_count_".$post_id;?>';
var content = "";
if(count == 1){
content = '<span class="comments_count">'+count+'</span> Comentário';
}else{
content = '<span class="comments_count">'+count+'</span> Comentários';
}
$(comments_count).html(content);
</script>
<?php
$this->load->model('model_user');
$user_img = $this->model_user->getUserPic($email);
echo '
<div>
<img style="height:32px;" src="'.site_url().'img/users/portfolio/'.$user_img.'" alt="" class="align-left" />
<p>
'.$this->model_mutamba->addSmilies($comment).'
<br>
<span class="comments_date">';
$date = date(date('Y-m-d H:i:s'));
$c_date = explode('-', substr($date,0,-8));
$date_num_word = monthNumToWords($date[1]);
echo substr($date, 11,-3)." ".$c_date[2]." de ". $date_num_word . " ". $c_date[0].'
</span>
</p>
</div>';
}
}else{
redirect("inicio");
}
}
Will appreciate any help,
Thanks in advance
This is happening because in your AJAX success callback, you are adding response to the output TWICE - once with .html() and then again with .append().
.html(response) removes the previous HTML in your webpage, thats why you dont see other comments and it adds response to it.
With .append(response), it is adding response to the bottom of the div, that is the reason you are seeing the response twice!
Remove $(output).html(response) from the success callback and it will work fine.
So, your function needs to look like this:
function comment(post_id,input){
var comment = $(input).val();
var output = '#comments_section_'+post_id;
if(comment != ""){
$.ajax({
type:"POST",
url:base_url+"mutamba/insertNewComment",
data: {comment: comment, post_id:post_id},
cache:false,
success:function(response){
$(output).append(response);
}
});
}
}
function comment(post_id,input){
var comment = $(input).val();
var output = '#comments_section_'+post_id;
if(comment != ""){
$.ajax({
type:"POST",
url:base_url+"mutamba/insertNewComment",
data: {comment: comment, post_id:post_id},
cache:false,
success:function(response){
//$(output).html(response); //Comment this line or the line below it.
$(output).append(response);
}
});
}
}
<div id="appenddiv">
<h1>append</h1>
</div>
<script type="text/javacript">
$("#appenddiv").append('<b>hello world</b>');
</script>

php variable for javascript ajax

I have a table that queries from a database that is updated frequently, and where
<span id="totalvotes1"></span>
and where
<span id="totalvotes2"></span>
I need to be able to be able to identify those in the line
success: function(data) { $('#totalvotes1').text(data); } });
in my ajax for each corresponding row queried... the way it is set up right now, my ajax will just display the information back into
<span id="totalvotes1"></span>
in the last row queried....
<?php
$sql = mysql_query("SELECT * FROM blogData ORDER BY id DESC");
$sql2=mysql_query("SELECT * FROM messages WHERE mod(mes_id,2) = 0 ORDER BY mes_id DESC");
$sql3=mysql_query("SELECT * FROM messages WHERE mod(mes_id,2) = 1 ORDER BY mes_id DESC");
$count_variable = 0;
while(($row = mysql_fetch_array($sql))AND($row2 = mysql_fetch_array($sql2))AND($row3 = mysql_fetch_array($sql3)) ){
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
$podcast = $row['podcast'];
$datetime = $row['datetime'];
$message1=$row2['msg'];
$mes_id1=$row2['mes_id'];
$totalvotes1=$row2['totalvotes'];
$message2=$row3['msg'];
$mes_id2=$row3['mes_id'];
$totalvotes2=$row3['totalvotes'];
?>
<table class="content">
<tr>
<td>
<div id="main">
<div id="left">
<span class='up'><img src="up.png" alt="Down" /></span><br />
<span id="totalvotes1"><?php echo $totalvotes1; ?></span><br />
</div>
<div id="message">
<?php echo $message1; ?>
</div>
<div class="clearfix"></div>
</div>
<div id="main">
<div id="right">
<br />
<span id="totalvotes2"><?php echo $totalvotes2; ?></span><br />
<span class='down'><img src="down.png" alt="Down" /></span>
</div>
<div id="message">
<?php echo $message2; ?>
</div>
<div class="clearfix"></div>
</div>
</td>
</tr>
</table>
<?php
}
?>
here is my general.js file
$(".vote").click(function()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var eData = $(this).attr("data-options");
var dataString = 'id='+ id + '&' + eData ;
var parent = $(this);
if(name=='up')
{
$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "up.php",
data: dataString,
cache: false,
success: function(data) { $('#totalvotes1').text(data); }
});
}
else
{
$(this).fadeIn(200).html('');
$.ajax({
type: "POST",
url: "down.php",
data: dataString,
cache: false,
success: function(data) { $('#totalvotes2').text(data); }
});
}
});
});
});
Possibly a earlier version of your latest question,
JSON encode, ajax and display back in html
In my answer on your latest question about this, I've given a detailed example of how to use JSON data between PHP and jQuery.

Categories