Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m trying to develop a show More applicaiton.Once the user click "showmore" button then new records supposed to be displayed.But When I execute the code the same records are always listed

What I have tried:

this is index.php



<?php

$con = new PDO('mysql:host=localhost;dbname=testing', 'root', '');
$str = $con->query('select * from konu order by id desc limit 2');

?>
<div id="rows">
<?php
foreach ($str as $row) {
    $id = $row['id']; ?>
<div class="item" id="<?php echo $id; ?>"><?php echo $row['header']; ?></div>
	<?php
}

?>
</div>
<?php
?>





<div class="menu" id="menu_<?php echo $id; ?>">
<span class="show" id="<?php echo $id; ?>">Show More</span>
<span class="load" style="display:none">loading</span>

</div>

<?php
?>
</body>
</html>


<script>


$(document).ready(function(){
	
		$(document).on('click','.show',function(){

	
			var id=$(this).attr("id");
			
			$('.load').show();
			
			
		$.ajax({
				url:"ajax.php",
				method:"post",
				data:{id:id},
				success:function(e){
					
					$('#rows').append(e);

				}
		})
	})
})
</script>


this is ajax php


<?php
$id = $_POST['id'];
$con = new PDO('mysql:host=localhost;dbname=testing', 'root', '');

$rtn = $con->query("select * from konu where id < '".$id."' order by id desc limit 2");

foreach ($rtn as $row) {
    ?>
<div class="item" id="<?php echo $row['id']; ?>"><?php echo $row['baslik']; ?></div>
<?php
}
Posted
Updated 13-Nov-18 7:48am

1 solution

I'm a bit of a control freak and run plain javaScript and php; no framework. Nonetheless, there are constants to debugging that transcend that.

Here's how to find your problem:

Display the value for $_POST['id'] you are sending. You can do this by, for example, echoing it to your screen even before you do your first show.

Now, click show and display the value being sent. Click it again. If you keep sending the same values then everything will remain the same.

Said in another way - are you send a query requesting the same data each time because of the value you POST for id?

Another thing: at least without jQuery, the data you send back via AJAX is sent back via the php ECHO command. You build a <div> in you r AJAX but how do you get it back to the calling page? This, too, would show as you sending the same data request to the AJAX call every time and it send the same data back - every time. HOWEVER, if you don't update anything on your page then you don't change your request.

If the computer keeps showing you the same answer then you probably keep asking it the same question. If you don't update your screen, for example, the question never changes. Also, I presume you have enough data in your table so it can send different results (i.e., > 2 records ?)
 
Share this answer
 
Comments
Member 3722539 14-Nov-18 5:04am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900