Jump to content

Filter with Pull Down Menu


usadarts

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
Link to comment
https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/
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
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
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

Archived

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

×
×
  • Create New...

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.