Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2012, 08:00 PM   PM User | #1
willscarlet
New Coder

 
Join Date: Aug 2012
Posts: 65
Thanks: 5
Thanked 0 Times in 0 Posts
willscarlet is an unknown quantity at this point
Query by multiple drop downs

I am looking to add multiple dropdown menus to a progam for searching (Like the kind of searching you would find at any car dealership website) had have that search the Database. Here is the code so far with only one drop down list. Any advice on how I would rewrite this example to make it work?:

PHP Code:
<label>Make:

<select name="make">
    <option value="Ford">Ford</option>
    <option value="Toyota">Toyota</option>
</select>
</label>

<label>Model:

<select name="model">
    <option value="Focus">Focus</option>
    <option value="Carola">Carola</option>
</select>
</label>



<input type="submit" name="search" value="Search"/>

</form>

</center>
<?php

include 'includes/db/connect.php';

if(isset(
$_POST['search']) || isset($_POST['submit']))
{     
    
$make $_POST['make'];
    
$model $_POST['model'];
    
    
$query "SELECT * FROM veh WHERE $make & $model ORDER BY year ASC";
willscarlet is offline   Reply With Quote
Old 11-26-2012, 08:08 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Take a look at this ... PHP/MySQL/AJAX

http://www.blueicestudios.com/chaine...hp-mysql-ajax/

I've used it before and it's nice.
To change the selections, you update the database table.

I also like the user interface. AJAX makes it nice.

Here is the 3-tier example:
http://blueicestudios.com/chained-se...es/three-tier/

I guess you can make as many tiers as you need.



.
mlseim is offline   Reply With Quote
Old 11-27-2012, 12:40 AM   PM User | #3
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
Quote:
Originally Posted by mlseim View Post
Take a look at this ... PHP/MySQL/AJAX

http://www.blueicestudios.com/chaine...hp-mysql-ajax/

I've used it before and it's nice.
To change the selections, you update the database table.

I also like the user interface. AJAX makes it nice.

Here is the 3-tier example:
http://blueicestudios.com/chained-se...es/three-tier/

I guess you can make as many tiers as you need.

.
+1 for AJAX. Especially if your second and subsequential dropdowns are depending on the previous one (which I guess is the case, as Focus' are only made by Ford, so selecting a Toyota won't give that option?).

If you want to program it yourself rather then using mlseims link (haven't checked it to be honest), have the <select> perform a JavaScript function on change or something.

PHP Code:
<select name="make" id="make" onchange="checkmodels()"
    <
option value="Ford">Ford</option
    <
option value="Toyota">Toyota</option
</
select
Code:
<script>
function check_models() {
var make = getElementById('make').value;
// get AJAX request ready, bla bla bla
// send AJAX request to functions.php?action=checkmodels
// if AJAX response is good, change selectlist
document.getElementById('models').innerHTML = ajax.responseText;
Where the functions file loads the corresponding models based on the make
PHP Code:
switch($_GET['action'] {
case 
'checkmodels':
$sql "SELECT * from models WHERE make = '{$_GET['action']}';
/* perform query, load 1 return variable with the HTML you want outputted, so 
 * <select name="
model"> 
 *   <option value="
Focus">Focus</option> 
 *   <option value="
Carola">Carola</option> 
 * </select> 
 */
echo $return;
break;

Right, this is by no way a tutorial, nor a definitive answer, just an outline of an AJAX request to show you that it really isn't that hard

Regards,
Martin
Thyrosis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:58 PM.


Advertisement
Log in to turn off these ads.