2012-10-03 44 views
0

我想改進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> 

我想不出如何使用降無需提交按鈕

回答

0

您可以使用JavaScript來操作下拉列表的內容。你所需要做的就是在單選按鈕上綁定點擊,當點擊它們中的一個時,使用Ajax調用從SQL數據庫檢索信息並修改選擇框的內容。

編輯

下面是一些代碼,你可以添加到您的網頁:

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

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

注意change可能無法正常工作。如果沒有,請嘗試使用click

+0

上午有試圖「綁定」的下拉菜單,從數據庫 – user1717124

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; 

} 
+0

我tried..but調用信息問題由於我是新手......無法實現該代碼。 – user1717124

+0

我玩過並玩過..更多的幫助...我將非常感激...大聲笑 – user1717124

+0

你不能發送形式參數爲PHP,或者你不知道如何返回一些數據從PHP(通過Ajax)和用結果填充一些html元素? – Djomla