Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

usadarts

Filter with Pull Down Menu

Recommended Posts

When page is first loaded, I would like all the infomation within the database shown. Then I would like to have a pull down menu to allow the user to filter by a specific field.

Here is what I have that does not work:

[code]<HTML>
<HEAD>
<script LANGUAGE="JavaScript">
<!-- Begin
function stateHandler(form){
var URL = http://www.anywebsite.com;
window.location.href = URL;
}
// End -->
</SCRIPT>
</HEAD>

<body bgcolor="#FFFFFF">
<form name="state">
<select name="state" size=1 onChange="javascript:stateHandler()">
<option value="">Filter By State
<option value="AL">Alabama
<option value="AK">Alaska
<option value="AZ">Arizona
<option value="AR">Arkansas
</select>
</form>[/code]

Below this code is the following PHP code:
[code]if($state == ''){
$queryt = "SELECT * FROM `affiliations` ORDER BY `org`";
$resultt = mysql_query($queryt,$dbh) or die(mysql_error());
} else {
$queryt = "SELECT * FROM `affiliations` ORDER BY `org` WHERE `state` = $state";
$resultt = mysql_query($queryt,$dbh) or die(mysql_error());
}[/code]

I was thinking that when a menu item is selected, the page will refresh itself with the correct filtered information.

Thanks,
David

Share this post


Link to post
Share on other sites
i think this cold is better:
[code]<HTML>
<HEAD>
<script LANGUAGE="JavaScript">
function stateHandler(form){
var URL = "www.page.com/page.php?state=" + form.state.value;
window.location.href = URL;
}
</SCRIPT>
</HEAD>
<body bgcolor="#FFFFFF">
<form name="state">
<select name="state" size=1 onChange="javascript:stateHandler(me)">
<option value="">Filter By State
<option value="AL">Alabama
<option value="AK">Alaska
<option value="AZ">Arizona
<option value="AR">Arkansas
</select>
</form>[/code]
And the below code:
[code]$state = $_GET['state'];
if($state == ''){
$queryt = "SELECT * FROM `affiliations` ORDER BY `org`";
$resultt = mysql_query($queryt,$dbh) or die(mysql_error());
} else {
$queryt = "SELECT * FROM `affiliations` ORDER BY `org` WHERE `state` = $state";
$resultt = mysql_query($queryt,$dbh) or die(mysql_error());
}[/code]
Bless it could help.. =)

D.Soul

Share this post


Link to post
Share on other sites
I see what I missed and understand it 100%. Thanks for the help.

However, when selecting something from the pulldown menu, nothing happens. Not even an error of somekind.


Thanks,
David

Share this post


Link to post
Share on other sites
I got part of it to work. If I select ALL then works fine. If I select a certain state, I receive this error:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `state` = `NJ`' at line 1[/quote]

Here is the particle code:
[code]
<script LANGUAGE="JavaScript">
function stateHandler(form){
var URL = "http://www.website.com/affiliations/index.php?state="+state.site.value;
window.location.href = URL;
}
</SCRIPT>
</HEAD>
<body>
<form name="state">
<select name="site" size=1 onChange="javascript:stateHandler()">
<option value="">Filter By State
<option value="ALL">All States
<option value="AL">Alabama
<option value="AK">Alaska
etc.......etc......etc......
</select>
</form>
</center>

<table><tr><td>

<?php
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("Default_Database");

$state = $_GET['state'];
if($state == 'ALL'){
$queryt = "SELECT * FROM `affiliations` ORDER BY `org`";
$resultt = mysql_query($queryt,$dbh) or die(mysql_error());
} else {
$queryt = "SELECT * FROM `affiliations` ORDER BY `org` WHERE `state` = `$state`";
$resultt = mysql_query($queryt,$dbh) or die(mysql_error());
}[/code]


Thanks,
David

Share this post


Link to post
Share on other sites
Try instead of this:
[code]$queryt = "SELECT * FROM `affiliations` ORDER BY `org` WHERE `state` = `$state`";[/code]
This:
[code]$queryt = "SELECT * FROM `affiliations` WHERE `state` = `$state` ORDER BY `org`";[/code]

Orio.

Share this post


Link to post
Share on other sites
Thank you....however, I am now getting a different message.....

Unknown column 'NJ' in 'where clause'

[code]$queryt = "SELECT * FROM `affiliations` WHERE `state` = `$state` ORDER BY `org`";[/code]


Never mind, got it....

$queryt = "SELECT * FROM `affiliations` WHERE `state` = '$state' ORDER BY `org`";


Thanks for all you help everyone,
David

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.