0

So I've setup my php table to be paginated by javascript (so that I dont have to refresh the webpage when looking through the pages of the table) and I'm trying to get a php variable $userid to be passed through the JS as my table needs this variable to echo the correct information from the sql database Any help is greatly appreciated, my code is as below

<?php include '../session.php'; 
$userid = $_GET['id'];
?>
<div id='retrieved-data'>
	<!-- 
	this is where data will be shown
	-->
 <img src="../images/ajax-loader.gif" />
</div>
<script type = "text/javascript" src = "includes/js/jquery-1.7.1.min.js"></script>
<script type = "text/javascript">
$(function() {
	//call the function onload
	getdata( 1 );
});
function getdata( pageno ){ 
	var targetURL = 'includes/pagination/userchatlog/search_results.php?page=' + pageno; 
	$('#retrieved-data').html('<p><img src="images/ajax-loader.gif" /></p>'); 
	$('#retrieved-data').load( targetURL ).hide().fadeIn('slow');
} 
</script>

asked Feb 24, 2016 at 15:05

1 Answer 1

2

First off, always use json_encode when outputting PHP variables in javascript. It ensures that the data is safely encoded, whatever it might be.

So it appears you just need to do something like this:

function getdata( pageno ){ 
 var userid = <?php echo json_encode($userid) ?>; 
 var targetURL = 'includes/pagination/userchatlog/search_results.php?userid=' + userid + '&page=' + pageno; 

This will make sure your userid gets sent back to the server every time you getdata. You'll be able to access it as $_GET['userid'] server side in search_results.php.

answered Feb 24, 2016 at 15:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.