Jump to content


Photo

Filter with Pull Down Menu


  • Please log in to reply
5 replies to this topic

#1 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 20 April 2006 - 06:47 PM

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:

<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>

Below this code is the following PHP 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());
}

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

Thanks,
David


#2 Darkness Soul

Darkness Soul
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts
  • LocationBrazil; São Paulo.

Posted 20 April 2006 - 07:29 PM

i think this cold is better:
<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>
And the below 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());
}
Bless it could help.. =)

D.Soul
(If something is wrong, please tell me. I'm learning this language. Thank you)

#3 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 20 April 2006 - 07:51 PM

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


#4 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 20 April 2006 - 08:53 PM

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:
<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());
}


Thanks,
David

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 21 April 2006 - 09:38 AM

Try instead of this:
$queryt = "SELECT * FROM `affiliations` ORDER BY `org` WHERE `state` = `$state`";
This:
$queryt = "SELECT * FROM `affiliations` WHERE `state` = `$state` ORDER BY `org`";

Orio.
Think you're smarty?

(Gone until 20 to November)

#6 usadarts

usadarts
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts
  • LocationLaurence Harbor, NJ

Posted 21 April 2006 - 02:16 PM

Thank you....however, I am now getting a different message.....

Unknown column 'NJ' in 'where clause'

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


Never mind, got it....

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


Thanks for all you help everyone,
David




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users