?
Solved

Javascript load content problem using php

Posted on 2015-01-22
3
Medium Priority
?
212 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

809 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