2012-03-16 91 views
0

我已經將下面的表格放在一起,允許用戶從他們從下拉菜單中選擇的日期檢索數據庫記錄。顯示「所有記錄」選項

<html> 
<head> 
<script type="text/javascript"> 

function ajaxFunction(name) 
{ 
var browser = navigator.appName; 
if(browser == "Microsoft Internet Explorer") 
{ 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

} 
else 
{// code for IE6, IE5 
xmlhttp=new XMLHttpRequest(); 

} 
xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
document.getElementById("my_div").innerHTML=xmlhttp.responseText; 
} 
} 

xmlhttp.open("GET","getrecords.php?dateoftrip="+name,true); 
xmlhttp.send(); 
} 

function getquerystring() { 
var form = document.forms['frm1']; 
var word = form.word.value; 
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring 
return qstr; 
} 


</script> 


<style type="text/css"> 
<!-- 
.style1 { 
    font-family: Calibri; 
    font-size: 14px; 
} 
--> 
</style> 
</head> 
<body> 

<form action="getrecords.php" method="get" name="frm1"> 

<table width="148" border="0"> 

<tr> 
<td width="152"><p class="style1">Select a date from below</p> 
    <div align="center"> 
    <?php 
include("db.php"); 

$query="select * from finds group by dateoftrip"; 
echo '<select onchange="ajaxFunction(this.value)">'; 
$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 

echo "<option name='name' value=".$rows['dateoftrip'].">".$rows['dateoftrip']."</option>"; 

} 
echo "</select>"; 
?> 
    </div></td> 
</tr> 
</table> 
</form> 
<div id="my_div"></div> 
</body> 
</html> 

這是檢索記錄的php腳本。

<?php 
include("db.php"); 
$dateoftrip= $_GET['dateoftrip']; 
$findname= $_GET['findname']; 
$finddescription= $_GET['finddescription']; 

$query="select * from finds where dateoftrip='$dateoftrip'"; 
echo "<table>"; 
$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 

echo "<tr>"; 
echo "<td>Find Name : </td>"; 
echo "<td>".$rows['findname']."</td>"; 
echo "</tr>"; 

echo "<tr>"; 
echo "<td>Find Description : </td>"; 
echo "<td>".$rows['finddescription']."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
?> 

我想擴展功能有點如果可能的話,加入了「所有記錄」選項,在下拉菜單中這顯然返回當前用戶所有記錄。

我一直在尋找這幾天,我還沒有找到一個例子,其中有這個附加功能。

我只是想知道是否有人可以提供一些指導,我可能會如何去這個請。

經修正代碼

<html> 
<head> 
<script type="text/javascript"> 

function ajaxFunction(name) 
{ 
var browser = navigator.appName; 
if(browser == "Microsoft Internet Explorer") 
{ 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

} 
else 
{// code for IE6, IE5 
xmlhttp=new XMLHttpRequest(); 

} 
xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
document.getElementById("my_div").innerHTML=xmlhttp.responseText; 
} 
} 

xmlhttp.open("GET","getrecords.php?dateoftrip="+name,true); 
xmlhttp.send(); 
} 

function getquerystring() { 
var form = document.forms['frm1']; 
var word = form.word.value; 
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring 
return qstr; 
} 


</script> 


<style type="text/css"> 
<!-- 
.style1 { 
    font-family: Calibri; 
    font-size: 14px; 
} 
--> 
</style> 
</head> 
<body> 

<form action="getrecords.php" method="get" name="frm1"> 

<table width="148" border="0"> 

<tr> 
<td width="152"><p class="style1">Select a date from below</p> 
    <div align="center"> 
    <?php 
include("db.php"); 

$query="select * from finds group by dateoftrip"; 
echo '<select onchange="ajaxFunction(this.value)"><OPTION name="name" value="ALLRECORDS">'; 
$result=mysql_query($query); 
while($rows=mysql_fetch_array($result)){ 

echo "<option name='name' value=".$rows['dateoftrip'].">".$rows['dateoftrip']."</option>"; 

} 
echo "</select>"; 
?> 
    </div></td> 
</tr> 
</table> 
</form> 
<div id="my_div"></div> 
</body> 
</html> 

回答

1

的 'ALL' 東西

echo '<select onchange="ajaxFunction(this.value)"><OPTION name="name" value="ALLRECORDS">All Records</option>'; 

處理您的查詢的 'ALL' 東西

if ($dateoftrip=="ALLRECORDS") { 
    $query="select * from finds"; 
} else { 
    $query="select * from finds where dateoftrip='$dateoftrip'"; 
} 
創建選項

在附註上,你應該看看SQL Injection,不是使用'*'選擇器,而是輸入你搜索的屬性的顯式名稱。

+0

非常感謝你回覆我的文章。我已經嘗試了您的代碼以降低dopwn菜單,並且不幸的是我無法顯示「所有記錄」選項。我使用修改後的代碼修改了我原來的帖子。請你可以看看它,讓我知道我哪裏出了問題?親切的問候 – IRHM 2012-03-16 15:46:35

+0

您需要關閉「所有記錄」選項......該行應顯示爲:'echo'