Question Locked

Using PHP, MySQL and Javascript.. please help!

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

OK.. so I developing in PHP using a MySQL database, but I think I need Javascript to enable the following.

I want a page with 3 dropdowns, the second dropdown is dependant on the first, and the third on the second. The options in the dropdown are found in my database.

The first query to MySQL for the first dropdown list is fine, but then I want to use the onchange event to trigger the next dropdown - which should query the database again using the id of the chosen field in the first dropdown.

I can get the id using javascript, but how do I get that var into a query string of PHP to use for a mysql query?

Does this make any sense?

Or is there an easier way of doing this?

Thanks!

5 replies

by thumbsup2

First, PHP is a server side language. It executes on the server. Javascript, on the other hand, executes at the client level in the browser. To pass javascript values to PHP, you have to send the form data back to the server and process it through the PHP script, then return the page to the browser with the updated values.

This means that your PHP script needs a check in it to see if it is the page first loaded or passed back to it from the Javascript with values in some hidden fields. It can get extremely complicated.

by deity_chooch

I think what you want here is the new technology (not really, but...) called AJAX (Asynchronous JavaScript and XML). Basically you want to create a page that when the SELECT element is changed using the JavaScript OnChange() function, a value is passed to an outside script. This script will process and return a value back to the JavaScript in your page. I guess in your situation, I'd have the OnChange() function send the ID of the selected OPTION to the PHP script, which will use that to run your MySQL query and return the results in a string format. You should be able to split() this string in JavaScript and use it to write the OPTIONs of the second SELECT element. Wash, rinse, and repeat. :D If you are unfamiliar with the workings of AJAX, I'd do a bit of reading to get an understanding of the XML HTTP Request object (the meat and bones of AJAX).

by fiifi

Yeah,I have been searching for a similar solution, if you got it now kindly share it with us.

This discussion is closed to new replies.