0

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?

mouthpiec
  • 3,923
  • 18
  • 54
  • 73
  • You aren't being very specific about *where* your problem actual lies. Please add some code samples or be more specific (e.g. "The AJAX query isn't returning anything"). – Josh K Apr 10 '10 at 17:47
  • I just found this tutorial which seems to solve my problem. http://www.plus2net.com/php_tutorial/php_drop_down_list.php – mouthpiec Apr 10 '10 at 18:22

3 Answers3

0

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
  • 28,364
  • 20
  • 86
  • 132
  • 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 Apr 10 '10 at 17:57
  • 1
    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 Apr 10 '10 at 18:44
0

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

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

mouthpiec
  • 3,923
  • 18
  • 54
  • 73