Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,135 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?