usadarts Posted April 20, 2006 Share Posted April 20, 2006 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"><!-- Beginfunction 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 Quote Link to comment https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/ Share on other sites More sharing options...
Darkness Soul Posted April 20, 2006 Share Posted April 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/#findComment-29118 Share on other sites More sharing options...
usadarts Posted April 20, 2006 Author Share Posted April 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/#findComment-29127 Share on other sites More sharing options...
usadarts Posted April 20, 2006 Author Share Posted April 20, 2006 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">Alaskaetc.......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 Quote Link to comment https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/#findComment-29145 Share on other sites More sharing options...
Orio Posted April 21, 2006 Share Posted April 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/#findComment-29283 Share on other sites More sharing options...
usadarts Posted April 21, 2006 Author Share Posted April 21, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7980-filter-with-pull-down-menu/#findComment-29335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.