views:

515

answers:

2

I have a combo box that will be loaded with a list of choices. When the user selects a choice, I need a javascript to simply run a query of mysql (obviously based on the user choice in the combo box) which will return a simple, discrete value that then needs to be displayed on the page next to the combo box. The query is nothing more than "select foo from tblexample where id = blah" (where blah is the combo box value). The value will be a simple number. If the user chooses a new value, then it should just requery and display the result. I'm open to reading the whole table in upon page load into an array or something too. I work in PHP, so I don't know the java; I was only hoping for a sample code bit; I can read and extrapolate most of the time. I just didn't want to have to put a submit button in a form and force the user to do that each time they look at a new combo box choice. I wanted a more seamless, quick display for them.

A: 

JavaScript is a client-side language. It will not run MySQL queries (safely, at least). Use PHP to dynamically create the HTML and JavaScript for the combo box.

PHP has an entire section of their documentation reserved for MySQL.

Eric
Thanks, I can code the PHP version of this in my sleep; I've done it that way plenty of times. I had wanted to do it without having to submit the page and wait for the refresh so the user could just see the results appear as they try different values for the drop down.
A: 

I think you are actually looking for a reference in Ajax programming using PHP on the backend and javascript on the front end.

My recommendation would be to look at using one of the excellent Javascript development frameworks. Great candidates would be JQuery or Prototype. They both give you solid libraries to simplify programming in javascript.

Rather than working with sample code, you'll probably get a lot further by developing javascript expertise. Ajax is complicated, and you'll need to at least get basic skills together before you can start to integrate javascript and PHP.

Here's a good query to get started- I'd recommend beginning with JQuery if you have to pick one.

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=ajax+php+jquery+tutorial

Tim Howland