views:

210

answers:

4

Hi,

I am writing the following in PHP: i would like to have 2 dropdownlists, where the second one is populated from a mySQL dB according to the choice made on the first dropdown list. Then I would like to use the values of the selected values of the dropdownlist to call another PHP page in which I will generate a db query.

Eg: List A: Car Manufactures (honda, nissan, etc) List B: Models (accord, civic, etc)

Then I will have a submit button, to POST the values in another PHP file? (I have the basics of how to perform the above without having a dynamic list, but I tried this with AJAX, but I am having problems to pass the value of the second dropdownlist)

Any tips of how I can perform the above?

A: 

You should have a separate PHP file (let's call it search.php) which will take a POST variable (let's call it make) and run a query which will spit out a list.

After it spits out the list (one model per line, ends with a \n) you should have a bit of javascript which will then throws that into the second drop down.

Hitting the submit button should just be a form POST to another PHP file.

Josh K
is it possible to provide an example? I am doing similar of what you are saying but I am not able to solve the issue. When I go the search.php and populate the dropdownlist, i am not able to keep the selected value once I go to the original page. Or do you suggest not to go to the original page?
mouthpiec
Don't go back to the original page or use another AJAX call. [This page is almost exactly what you're trying to do](http://autooneinc.com/getaquote.php). You could probably *borrow* some of the JS and flow control idea.
Josh K
A: 

I'd suggest to make it without AJAX at first.

just make a first form using GET method. So, after submit this form, you'll land with a classical ?choice=1 query string on the second script. So, you can use $_GET['choice'] (properly sanitized of course) to query a database and populate second select.

Don't forget to add

<input type="hidden" name="choice" value="<?php echo htmlspecialchars($_GET['choice'])?>">

to the second form

Col. Shrapnel
A: 

solution found here: http://www.plus2net.com/php_tutorial/php_drop_down_list.php

mouthpiec
A: 

You can build your drop down boxes with form builder and save your time.