Reputation: 995
I have created a news area where i have display 3 thumbs of news at a time the source is from mysql database. i have used the query like this:
$eu = 0;
$limit = 3;
$query2=mysql_query("SELECT * FROM news
ORDER BY ID
DESC LIMIT $eu,$limit");
now i have also create two buttons with like this
i want to use this buttons for display the next 3 thumbs onclick and prev 3 thumbs. but not able to change the value inside the query i.e $eu.
how can i change the $eu value inside the mysql query with javascript or any other suggestion and also i dont want to do this with reload of page with passing query parameters. i want it without reloading of page
Upvotes: 0
Views: 793
Reputation: 4601
Try this //PHP
$count=3;
$query= 'select * from news order by id desc ' ;
$limit_str=" limit 0,$count";
if(isset($_POST['page']))
{
$lim=$_POST['page'];
$offset=$lim[0]*$count;
$lim_str=" limit $offset, $count";
$query.=$lim_str;
}
//Form
<form method='post' >
< input type='submit' name='page[]' value='1' />
< input type='submit' name='page[]' value='2' />
< input type='submit' name='page[]' value='3' />
</form>
<?php
// Display logic
?>
Upvotes: 0
Reputation: 2228
You cannot change the value of any PHP script directly from Javascript because PHP is parsed prior to the page being sent to the browser (i.e. server-side) and Javascript is parsed and execute by the browser (i.e. client-side).
There are 2 main ways this sort of thing is accomplished. If you really want to affect the PHP variable value then you'll have to reload the page (for example using an HTML form). Since you said you don't want to do that, then as others have suggested, you'll want to look at AJAX.
In a nutshell you'll use Javascript to send a request to a PHP page which will run the query you have above (and you can pass along a new value to plug into $eu in the form of POST or GET data) and return the results which you will then use to update the display on your page.
Upvotes: 0
Reputation: 237
You can't change a php variable from Javascript simply because the server side script is already run, and javascript is a client side script.
What I suggest would be, use javascript to send an ajax request to a PHP scriptthat accepts $eu as a parameter.
In that PHP, do the query with the $eu, and print out the search results.
In your javascript, catch this result upon success ajax request and process/replace certain elements in your HTML with the result.
Upvotes: 0
Reputation: 3569
You'll need to do an ajax call, pass in the value for $eu (eg, 3, 6, 9 etc) that you need, and pass back the contents of the news feed, to replace the existing lot.
There are many ways you can do this and which is easiest depends on the frameworks you are using. Google is your friend here, there are plenty of tutorials etc.
Upvotes: 1