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.
Related
I have below code to get more results on page scroll.
It works fine on localhost and on server laptop/desktop.
It does not work on mobile and does not load more results on scroll.
I cannot figure it out why this is happening or what is causing this not to work on mobile.
<?php
$getItemLID = $dba->prepare('SELECT MAX(id) as id FROM items
where status = ? AND ename like ?');
$getItemLID->bind_param('ss', $status,$param);
$getItemLID->execute();
$resultLID = $getItemLID->get_result();
$rowLID = $resultLID->fetch_assoc();
$thelastid = $rowLID['id'];
$status = 1;
$getscategory = $dba->prepare('SELECT * FROM mytable
where status = ?
order by id asc');
$getscategory->bind_param('s', $status);
$getscategory->execute();
$resultGSC = $getscategory->get_result();
while ($rowGSC = $resultGSC->fetch_assoc()) {
$scid = $rowGSC['id'];
$scename = $rowGSC['ename'];
?>
<div class="message_box" data-id="<?php echo $scid; ?>" style="padding-right: 5px;">
<div class="portfolio-item-wrap" style="border-radius: 3%;">
<span class="portfolio-description">
<h3><?php echo $scename; ?></h3>
</span>
</div>
</div>
<?php } ?>
<div id="msg_loaderw" style="display: none;">
<div class="card">
<div class="card-body">
<div class="d-flex align-items-center">
<strong>Loading...</strong>
<div class="spinner-border ml-auto" role="status" aria-hidden="true">
</div>
</div>
</div>
</div>
</div>
<div id="msg_loader">
</div>
<script>
$(document).ready(function () {
$(window).scroll(function () {
var thislastid = "<?php echo $thelastid; ?>";
if($(window).scrollTop() == ($(document).height() - $(window).height())) {
$("#msg_loaderw").show();
var msg_id = $(".message_box:last").data("id");
$.ajax({
type: "POST",
url: "inc/items/search_items_get.php",
data: {msg_id: msg_id},
cache: false,
success: function (data) {
//Insert data after the message_box
$(".message_boxx").append(data);
if (msg_id == thislastid) {
$("#msg_loaderw").hide();
$("#msg_loader").html('<hr><div class="card"><div class="card-body"><div class="align-items-center"><strong><center>That is all what we have for now.</center></strong></div></div></div>');
}
}
});
}
});
});
</script>
Below is : inc/items/search_items_get.php
<?php
include ('../default/db.php');
if (isset($_POST['msg_id']) && isset($_POST['msg_id']) !== NULL) {
$msg_id = $_POST['msg_id'];
$status = 1;
$limit = 12;
$getItem = $dba->prepare('SELECT * FROM mytable
where id > ? AND status = ?
order by id asc');
$getItem->bind_param('ss', $msg_id,$status);
$getItem->execute();
$resultItem = $getItem->get_result();
while ($rowItem = $resultItem->fetch_assoc()) {
$itemID = $rowItem['id'];
$itemName = $rowItem['ename'];
?>
<div class="message_box" data-id="<?php echo $itemID; ?>" style="padding-right: 5px;">
<div class="portfolio-item-wrap" style="border-radius: 3%;">
<span class="portfolio-description">
<h5><?php echo $itemName; ?></h5>
</span>
</div>
</div>
<?php
}
} else {
echo "Message ID is empty";
}
?>*emphasized text*
It seems it's a script issue just try changing the code a bit , hopefully it will work:
<script>
$(document).ready(function () {
$(window).scroll(function () {
var scrollHeight = $(document).height();
var scrollPosition = $(window).height() + $(window).scrollTop();
if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
$("#msg_loaderw").show();
var msg_id = $(".message_box:last").data("id");
$.ajax({
type: "POST",
url: "inc/items/search_items_get.php",
data: {msg_id: msg_id},
cache: false,
success: function (data) {
//Insert data after the message_box
$(".message_boxx").append(data);
if (msg_id == thislastid) {
$("#msg_loaderw").hide();
$("#msg_loader").html('<hr><div class="card"><div class="card-body"><div class="align-items-center"><strong><center>That is all what we have for now.</center></strong></div></div></div>');
}
}
});
}
});
});
</script>
I'm trying fetch data count from database to HTML div using PHP, jQuery and AJAX, but code below is not working.
I just started learning ajax please help. Thank you!
index.php
<div class='col-lg-3 col-md-6'>
<div class='panel panel-primary'>
<div class='panel-heading'>
<div class='row'>
<div class='col-xs-3'>
<i class='fa fa-microphone fa-5x'></i>
</div>
<div class='col-xs-9 text-right'>
<!-- here i want to fetch result -->
<div id="leadmonth"></div>
<div>Total Leads</div>
</div>
</div>
</div>
</div>
</div>
Jquery
$(document).ready(function() {
$.ajax({
type:'POST',
url:'php_action/resourceqareportdashboardcount.php',
dataType : 'json',
success:function(html){
$('#leadmonth').html(html);
}
});
PHP
<?php
// This Month Count
require_once 'db_config.php';
$output= '';
$sql = "SELECT * FROM leads WHERE YEAR(leadcreationdate) = YEAR(NOW()) AND MONTH(leadcreationdate)=MONTH(NOW())";
$query = $connect->query($sql);
$totalleads = mysqli_num_rows($query);
if ($monthtotalleadscount > "0") {
$output = "<div class='huge'>$totalleads</div>";
} else {
$output = "<div class='huge'>0</div>";
}
// database connection close
$connect->close();
echo json_encode($output);
$(document).ready(function() {
$.ajax({
type:'POST',
url:'php_action/resourceqareportdashboardcount.php',
// dataType : 'json',
success:function(html){
$('#totalleadsmonth').html(html);
}
});
});
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,
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
How can I print out data from a while loop with JSON?
Currently, I have this in the message.php
<script type="text/javascript">
$('#pmid<?php echo $convoData['id']; ?>').click(function(){
$.ajax({
type:"GET",
url: "/index.php?i=pm&p=rr",
dataType:'json',
data: {id:"<?php echo $convoData['id']; ?>"},
success : function(res){
if(res){
$( "#name").html(res.name);
$( "#post").html(res.response);
}
}
});
});
</script>
<span id="name"></span>
<ul id="post">
</ul>
And in the get.php file, I have this:
$name=$getUser['username'];
while($postData=mysql_fetch_assoc($postsql)){
$post = '<li>
<img width="30" height="30" src="images/avatar-male.jpg">
<div class="bubble">
<a class="user-name" href="">123</a>
<p class="message">
'.$postData['text'].'
</p>
<p class="time">
</p>
</div>
</li>';
}
$arrRet = array();
$arrRet['name'] = $name;
$arrRet['post'] = $post;
echo json_encode($arrRet);
die();
So far, the $name value is working. I can print that out to #name. I just can't figure out how to print out the $post while loop to #post
You need to add your MySQL results into an array and then make json with this
then in your JavaScript code you prints your data (post) with a for/foreach loop
your Get.php should like this :
$name=$getUser['username'];
$result = array();
while($postData=mysql_fetch_assoc($postsql)){
$result[] = $postData['text'];
}
$arrRet = array();
$arrRet['name'] = $name;
$arrRet['post'] = $result;
echo json_encode($arrRet);
die();
and message.php :
<script type="text/javascript">
$('#pmid<?php echo $convoData['id']; ?>').click(function(){
$.ajax({
type:"GET",
url: "/index.php?i=pm&p=rr",
dataType:'json',
data: {id:"<?php echo $convoData['id']; ?>"},
success : function(res){
if(res){
$( "#name").html(res.name);
var posts = res.post;
for(var i in posts)
{
var post = '<li><img width="30" height="30" src="images/avatar-male.jpg"><div class="bubble">
<a class="user-name" href="">123</a><p class="message">'
+ posts[i] +
'</p><p class="time"></p></div></li>';
$( "#post").append(post);
}
}
}
});
});
</script>
<span id="name"></span>
<ul id="post">
</ul>
Good luck