![]() |
PHP + MYSQL filter query results dynamically (without sending back to the server)
Hello, I have a database I am trying to perform some filtering on. The default display is all of the records (working - or was before I tried what I'm about to post), and I have some inputs (drop downs, text fields) I'd like to change the state of and have the results update - hopefully without the use of a submit button. Here's basically what I'm trying to do.
Basic PHP code: PHP Code:
I'm more used to the javascript DOM, and it seems like I do need to pass the results back to javascript somehow to do this without submitting a form - but then I think I will run in to a problem with getting js to update the query results from the database. Any help would be appreciated. |
If you don't want to submit form use AJAX
|
Your very first line of code should be throwing up an error:
Code:
echo "<th><select name='A' id='A' onChange='" . theAFunction() . "'>Next <th> is used in tables. Should not be here. Code:
WHERE aye LIKE '%' ...Why don't you post the complete code? The html and the php so you can see the problem. You have to submit a form, that's how html talks to php to update or select information from a database. |
Quote:
His very first line of code is actually 4 physical lines: Code:
echo "<th><select name='A' id='A' onChange='" . theAFunction() . "'> *************** Quote:
Quote:
Quote:
**************** His problem is simple, he calls theAfunction( ) in doing that first echo, which I assume does what it is supposed to do by setting the value of $a to match $_POST["A"], but then he turns around *AFTER THAT* and *RESETS* $a back to $. I *think* he just needs to put the initialization of $a, $b, etc. *BEFORE* that first call to theAfunction(). But maybe not. I don't follow the logic (or illogic) of this page, at all. |
You've nailed it Old Pedant, but for the last part, where I call the function is where it falls over. I could, in that function set $a = 'something' just not a $_POST[]. With php, $_POST causes the parser to expect there to be some data that was sent to the server using the same method, and tries to return it. I may have confused the issue there, but my intent was to show the hoped-for end-result.
The problem is basically that I have data fetched from a table using php, but I'd like to perform client side filtering on it via javascript (edit: well, preferably php but from the responses here js looks like the best bet). I need some tool to requery the data client side after it is fetched with php. AJAX will apparently do it (as BubikolRamios) pointed out, but I'm considering JQuery as well. I've been putting off tackling JQuery, so this might be a good time to start. |
Ummm...AJAX doesn't do any filtering unless you write it. For that matter, neither does jQuery.
More than likely, you *SHOULD* do the filtering in PHP/MySQL. Why not something simple: Code:
$a = isset($_POST["a") ) ? $_POST["a"] : '%'; Usually, people send AJAX requests as part of the query string, though, so then you would use: Code:
$a = isset($_GET["a") ) ? $_GET["a"] : '%'; It's probably a TINY bit more efficient to code those as Code:
$a = $_GET["a"]; if ( ! isset($a) ) { $a = "%"; } |
Quote:
Thanks as always Old Pedant. |
So I've added a variable that I can increment in my while loop to give each <tr> and <td> a unique id and removed the calls to the php functions, I'm wondering if I can write the functions in javascript and do some logic to set the css display property of rows I want to hide. something like this:
PHP Code:
|
But what about re-ordering rows? That is, allowing the user to change the "sort order" of the data?
If you are going to do this, why not do it right? The right way is to create an array of objects that JS code receives as same and then put in the sorting, etc., all in the JS code. It's really not too hard. |
Maybe (likely) I've missed something fundamental. What do you mean?
|
Okay, I think you know I don't use PHP.
And I'm sure there is a better way to do what I am about to show you. In PHP, I think you can create an array of objects and then call a method that will produce the same array in JS code in JSON notation. But since I don't know how to do that, I'll use brute force. But first, let's use a real world example, instead of your hokey "aye" and "bee". Back later. |
FWIW, I was able to achieve my goal (obviously without sorting) using jquery. I don't think it would have been much more difficult using vanilla js though. I wrote a function that loops through the rows, if the innerHTML of a column in the row doesn't match the selected index of the drop down it changes the class of the row and hides it.
This won't really make sense since I haven't modified it to match my example, but here it is for now: Code:
function threadSearch() |
Here's my function edited to match the example:
Code:
function theAfunction() |
Okay...here is the HTML and JavaScript code. Have to go to Dr. appointment. Back later to show you how to integrate PHP into it.
In the mean time, copy/paste it to your own machine and try it out. Click on the column headings. Click the same one again. Change the <select> choice. Play with it. Does it do what you would want? Code:
<!DOCTYPE html> |
Okay, so the only part of all that code that PHP needs to be concerned with is here:
Code:
// Use PHP (or ASP or JSP or whatever) to create an array of EmployeesAgain, remembering that I don't use PHP, it should go something like this: Code:
... all the HTML and JavaScript above this point is untouched ...Naturally, it would be easy to add more filters, if you wanted. |
| All times are GMT +1. The time now is 03:42 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.