Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

nicholas maddren
nicholas maddren
12,793 Points

How can I filter PDO queries with JS/jQuery?

Hey guys, I want to be able to filter PDO and MySQL queries that are returned to my front end using JavaScript or a plus would be jQuery.

First I will explain what I am actually doing...

I am developing an ecommerce styled listing page, all of the data in my database is displayed into the front end and users can view vehicles that are in that table. I currently have all vehicles in a list using PDO just like this example:

http://vans.autotrader.co.uk/search?

As you can see the filtering module on the left of that example uses JavaScript to display the options available.

My question is how is the JS having an affect on what the PHP is returning from the database there must be some sort of connection between JS and PHP but I am struggling to understand how this is possible?

1 Answer

Kevin Korte
Kevin Korte
28,148 Points

There is a connection between JS/jQuery and PHP, and it's called AJAX.

I used user choices in a select dropdown, to filter the results of a Wordpress loop. I have this site still in development but it's live at http://www.machinecheatsheet.com/.

Basically have jQuery get the value(s) of whatever the user selected from the form element, and it sends it to some PHP script on your server. That php script than retrieves the values from the AJAX call, using the POST or GET method. Than, once those values are set, you run a script that would/could do a new PDO MySQL query using the information you got from the form data. The query happens, and you send it the browser via AJAX. All this can happen without a page refresh. However, it can take awhile to get it right.

nicholas maddren
nicholas maddren
12,793 Points

It sounds like a lot more work needs putting in to grab data without a page refresh, would it be easier to load data with a page refresh?