Solved

Javascript load content problem using php

Posted on 2015-01-22
3
182 Views
Last Modified: 2015-01-23
I wish to display products in my table like twitter more tweets load style but here user in my site should click on button to see more products, here I am facing problem when I click on button products loading for first time after next click button not working so I am not getting products, so please help me how to solve this issue

javascript Code is below
$('.more').click(function(){
  var ID = $(this).attr("id");
  var cats = $('input[name=cats]').val();
  $("#more"+ID).html('<img src="<?=ROOT;?>resources/moreajax.gif" />');
  $.post('<?=ROOT;?>product/dataLoad', {id:ID,cats:cats}, function(data){
    if(data) {
      $('ol#updates').append(data);
      $("#more"+ID).remove();
    }
    else{
      $(".morebox").html('The End');// no results
    }
  });
  return false;
});

Open in new window

My Controller Function Code
public function dataLoad(){
        if(isset($_POST['id']))
    {
    $lastmsg=$_POST['id'];
    $str = $_POST['cats'];
    $query=mysql_query("select * from product where id > '".$lastmsg."' AND category_id IN ($str) order by id asc limit 2");
    $numrows = mysql_num_rows($query);
    while($ob = mysql_fetch_array($query))
    {
                $id = $ob['id'];
    ?>
    <a rel="canonical" href="<?=ROOT?><?=$this->user->sluggify($this->user->getCategoryName($ob['category_id']))?>/<?=$ob['url'];?>.html">
    <div class="product cprod" data-id="<?=$ob['id'];?>">
    <strong><h5 class="cube"><?=$ob['name'];?></h5></strong>
    <?php $img = $this->user->getUploadAction($ob['id']); ?>
    <img src="<?=ROOT.'uploads/'.$img?>" data-img="<?=$img?>">
    <p>
    Rs <span><?php
    $price = $this->user->getLowestPrice($ob['id']);
    echo isset($price)?$price:0;
    ?></span>
    </p>
    </div>
    </a>
    <?php
    }//while
    ?>
    
            <div id="more<?php echo $id; ?>" class="morebox" style="clear:both">
    <input type="hidden" name="cats" value="<?=$str;?>" />
    <a href="#" class="more" id="<?=$id;?>">Load More</a>
    </div>
        <?php
                
            }
        }

Open in new window

View Page Code
<ol id="updates">
      <?php
      $str = implode(",",$cats);
      $query = "select * FROM product WHERE category_id IN (".$str.") order by id asc limit 2";
      $result=mysql_query($query);
      $numrows = mysql_num_rows($result);
      while($ob = mysql_fetch_array($result))
      {
            $id = $ob['id']
      ?>
      <a rel="canonical" href="<?=ROOT?><?=$this->user->sluggify($this->user->getCategoryName($ob['category_id']))?>/<?=$ob['url'];?>.html">
      <div class="product cprod" data-id="<?=$ob['id'];?>">
      <strong><h5 class="cube"><?=$ob['name'];?></h5></strong>
      <?php $img = $this->user->getUploadAction($ob['id']); ?>
      <img src="<?=ROOT.'uploads/'.$img?>" data-img="<?=$img?>">
      <p>
            Rs <span><?php
            $price = $this->user->getLowestPrice($ob['id']);
            echo isset($price)?$price:0;
            ?></span>
            </p>
            </div>
      </a>
      <?php
      }
      ?>
      </ol>
            <div id="more<?php echo $id; ?>" class="morebox" style="clear:both">
      <input type="hidden" name="cats" value="<?=$str;?>" />
      <a href="#" class="more" id="<?php echo $id; ?>">Load More</a>
      </div>

Open in new window

0
Comment
Question by:veeru bolem
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 40565941
You want to change

$('.more').click(function(){

to

$("#updates").on("click",".more",function(){

assuming #updates is a parent of the .more objects
0
 

Author Comment

by:veeru bolem
ID: 40566071
Oh Great It is working Thank You So Much.
0
 

Author Comment

by:veeru bolem
ID: 40566076
I've requested that this question be closed as follows:

Accepted answer: 0 points for veeru bolem's comment #a40566071

for the following reason:

" $("#updates").on("click",".more",function(){ " is solved my problem.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question