使用下拉框优化结果

时间:2012-10-03 13:00:10

标签: php

我想使用下拉框优化mysql查询的结果...现场网站在这里: http://www.halfwayenterprises.com/search/tyrell.php

     <form name="doublecombo" action="" method="POST">

    <label for="status">Status:</label>

<label for="current">
<input class="radio_style" id="current" checked="checked" name="status" type="radio" value="current">
Current
</label>

<label for="obsolete">
<input class="radio_style" id="obsolete" name="status" type="radio" value="obsolete">
Obsolete
</label>

<label for="both">
<input class="radio_style" id="both" name="status" type="radio" value="both">
ALL
</label>

<br /><br />




<select name="category" size="1" onChange="redirect(this.options.selectedIndex)">
<option value="null">Category</option>
<option value="asset management">Asset Management</option>
<option value="budget">Budget/Finance</option>
<option value="central office">Central Office</option>
<option value="disposal">Disposal</option>
</select>





<select name="subcategory">
<option value="null">Sub-Category</option>
<option value="Portfolio">Portfolio</option>
<option value="Pricing">Pricing</option>
<option value="Valuation">Valuation</option>

<option value="Disposal">Disposal</option>





</select>
<input name="submitted" type="submit" value="GO">

<br />









Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option id="title" VALUE="title">Title</option>
<Option id="poc" VALUE="poc">POC</option>
<Option id="purpose" VALUE="purpose">Purpose</option>

</Select>

<input type="hidden" name="searching" value="yes" />





<input name="submitted" type="submit" value="GO">

</form>


<select name="filter1">
<option value="az">Sort by A-Z</option>
<option value="date">Sort by Date</option>
</select>

<select name="filter2">
<option value="office">Sort by Office</option>
<option value="p">P</option>
<option value="pt">PT</option>
<option value="pf">PF</option>
</select>

<select name="filter3">
<option value="mandatory">Mandatory</option>
<option value="nonmandatory">Non-Mandatory</option>
<option value="combined">Combined</option>
</select>


</form>





</p>

<script>
<!--

/*
Double Combo Script Credit
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free JavaScripts here!
*/

var groups=document.doublecombo.category.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()

group[0][0]=new Option("Sub Category")


group[1][0]=new Option("Portfolio")
group[1][1]=new Option("Pricing")
group[1][2]=new Option("Valuation")

group[3][0]=new Option("Central Office")
group[4][0]=new Option("Disposal")

var temp=document.doublecombo.subcategory

function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[x][i].text,group[x][i].value)
}
temp.options[0].selected=true
}

function go(){
location=temp.options[temp.selectedIndex].value
}
//-->
</script>

<?



mysql_connect("localhost", "name", "pasword") or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error()); 

if(isset($_POST["submitted"])){

$status = $_POST['status'];


$category = $_POST['category'];
$subcategory = $_POST['subcategory'];





echo '<div class="status_div">';

if($status=='current')

{
echo "<h2>Results</h2><p>";

$res = mysql_query("SELECT * FROM material WHERE status='$status' AND category='$category' AND subcategory='$subcategory' ORDER BY `documentid` ASC");

while ($row = mysql_fetch_assoc($res)) {


echo '<div class="current">';



echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];

echo '</div>';

echo '<br>';


}

} if ($status=='obsolete')


{
echo "<h2>Results</h2><p>";
$res = mysql_query("SELECT * FROM material WHERE status='$status' AND category='$category' ORDER BY `documentid` ASC ");

while ($row = mysql_fetch_assoc($res)) {



echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];



echo '<br>';


} 




} if ($status=='both')  

{

echo "<h2>Results</h2><p>";
$res = mysql_query("SELECT * FROM material WHERE status1='both' AND category='$category' ORDER BY `documentid` ASC");

while ($row = mysql_fetch_assoc($res)) {
echo '<div class="">';



echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];

echo '</div>';

echo '<br>';

} 

}








echo '</div>';

}















$field = @$_POST['field'] ;
$find = @$_POST['find'] ;
$searching = @$_POST['searching'] ;
$status = $_POST['status'];

//This is only displayed if they have submitted the form 
if ($searching =="yes") 
{ 
echo "<h2></h2><p>"; 


//If they did not enter a search term we give them an error 
if ($find == "") 
{ 

echo "<a href='tyrell.htm'>Return</a>";
exit; 
} 


// We preform a bit of filtering 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 

//Now we search for our search term, in the field the user specified 


if($status=='current'){

$data = mysql_query("SELECT * FROM material WHERE status='$status' AND lower($field) LIKE'%$find%' LIMIT 0,30");

while ($row = mysql_fetch_assoc($data)) {

echo '<div class="current">';

echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];

echo '</div>';

echo '<br>';

}




}

if ($status=='obsolete'){

$data = mysql_query("SELECT * FROM material WHERE status='$status' AND lower($field) LIKE'%$find%' LIMIT 0,30");

while ($row = mysql_fetch_assoc($data)) {

echo '<div class="obsolete">';

echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];

echo '</div>';

echo '<br>';

} 




} if ($status=='both'){

$data = mysql_query("SELECT * FROM material WHERE status1='both' AND lower($field) LIKE'%$find%' LIMIT 0,30");

while ($row = mysql_fetch_assoc($data)) {

echo '<div class="">';

echo $row ['documentid'].' - '.$row['category'].' - '.$row['title'].' - '.$row['status'];

echo '</div>';

echo '<br>';

} 

}









//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 


?>

<p align="center"><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
Kit</a></font></p>'
</body>
</html>

我没有想到如何在没有提交按钮的情况下使用下拉菜单

2 个答案:

答案 0 :(得分:0)

您可以使用JavaScript来操作下拉列表的内容。您所需要做的就是在单选按钮上绑定单击,当单击其中一个时,使用Ajax调用从SQL数据库中检索信息并修改选择框的内容。

修改

以下是您可以添加到网页的一些代码:

$('form[name=doublecombo] input').change(function() {

    $('form[name=doublecombo]').submit();
});

请注意change可能不起作用。如果没有,请尝试使用click

答案 1 :(得分:0)

我想我知道你需要什么...... 使用jquery在您的页面上提交表单。

像这样......

$("submitted").click(function() {

    var url = "file.php";

    $.ajax({
           type: "POST",
           url: url,
           data: $("doublecombo").serialize(), // serializes the form's elements.
           success: function(data)
           {
               // populate box or div with your result
           }
         });

    return false; // avoid reload.
});

// PHP部分

   if($_POST['someField']){

    $result = //Do query and staff and return any type of resposne you will handle in ajax success

    echo $result;
    exit;

}