[SOLVED] PHP/MySQL run query on button press/click


Was wondering if it’s possible to run/re-run a mysql query when a button/link is pressed/clicked?

Basically, I run a query that limits the results to the first 7, with a button below the results to show more results. When the button is clicked/pressed, I need to re-run the query and set the limit to, say 20.

Any help/advice on how to do this would be greatly appreciated. Thanks in advance, S.


The code to run the query and produce the results is on the server and the button is on the browser so you need some way to communicate back to the server that you want more results. You can either use an Ajax call and update your page’s contents dynamically or just make it a <a> link and GET another page with more results on.

You already have code that returns 7 results and show them on a page – let’s say your page to show the seven is https://www.blah.com/show. You could add a parameter to your page that accepts the number of results to show, e.g. https://www.blah.com/show?num=7. Now instead of hardcoding the SQL to read seven results, you first read the parameter and use that number in your query. The link to show more results can now be <a href="https://www.blah.com/show?num=20">Show more</a>

The Ajax option could be similar, but instead of using a <a> to retrieve the new page from the server you would call it using (in jQuery) $.ajax() and would insert the results into your page. This is slightly more complicated than the first example.

Answered By – Steve Claridge

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *