2013-08-16 49 views
0

我想要多個下拉列表,它們在數據庫中執行搜索並在網頁的表格中回顯信息。Ajax從第二選擇和數據庫中提取信息

從教程我管理這個,但只有一個選擇的選擇。我努力使第二個下拉列表改進搜索條件。

  • 下拉列表1應該是在數據庫中搜索所選值的選項。

  • 下拉列表2應向AND添加AND條件並使用第二個值來優化搜索。

下面的代碼不會從數據庫中獲取有關更改的任何信息。如果我刪除AND語句,它將從數據庫中提取信息。

HTML:

<!DOCTYPE html> 
<html> 

<head> 

    <title> Vault of Faults-Fault Search </title> 

    <link rel="stylesheet" type="text/css" href="mystyle1.css"> 

     <script src="dropdownfix1.js"></script> 

</head> 

<body> 

    <div id=container> 

    <div id=header> 
     <div id=headdiv> 


      <form id=login name="login" action="login.php" method="post"> 
        <fieldset class="field_set"> 
        <legend>Administrator Login:</legend> <!-- legeng tage creates a header title for the fieldset box, filedset pulls all data in the tag to gether with a box around it. --> 

        UserName: <input type="text" name="username"> <br> 

        Password:<br> <input type="password" name="password"> <br> 

        <input type="submit" value="Login"> <input type="Button" onClick="parent.location='addafix.php'" Value="Add a Fix"> 


        </fieldset> 
        </form> 


     </div> 
    </div> 

     <div id=content> 
     <div id=maincontent> 

     <div id=select> 
    <form> 
     <fieldset class="field_set2"> 
     <legend>Quicklink Vault of Faults Search</legend> 
     Product: 



    <select name="Product" onchange="showUser(this.value)"> 

     <option value="0">Select Product</option> 
     <option value="1">Merlin</option> 
     <option value="2">Encoder</option> 
     <option value="3">Mac Live</option> 
     <option value="4">Windows Live</option> 
     <option value="5">Windows S&F</option> 
     <option value="6">Mac S&F</option> 
     </select> 

     <select name="Product_Issue" onchange="showIssue(this.value)"> 

     <option value="0">Select Issue</option> 
     <option value="1">Preview</option> 
     <option value="2">Live Reciever</option> 
     <option value="3">Mac Live</option> 
     <option value="4">Windows Live</option> 
     <option value="5">Windows S&F</option> 
     <option value="6">Mac S&F</option> 
     </select> 






     <input type="submit" value="Search"> 
     </fieldset> 
    </form> 


    </div> 
      <div id=list> 
      <div id="txtHint"><b>Person info will be listed here.</b></div> 
      </div> 
     </div> 



     </div> 



    </div> 




</body> 

</html> 

的JavaScript:

function showUser(str) 
{ 
if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","getuser.php?q="+str,true); 
xmlhttp.send(); 
} 


function showIssue(str) 
{ 
if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","getuser.php?p="+str,true); 
xmlhttp.send(); 
} 

PHP:

<?php 

$q=$_GET["q"]; 
$p=$_GET["p"];      
require 'connection.php'; 
mysqli_select_db($con,"Faults"); 

//where statement in the sql syntax will select where in db to get infor, use AND to add another condition 
$sql="SELECT Products.Product_Name, Versions.Version, Platform.Platform_Name, Issues.Issue, Issues.Sub_Issue, Issues.Fix 
FROM Solutions INNER JOIN Products ON Solutions.Product = Products.Product_id 
INNER JOIN Versions ON Solutions.Product_Version = Versions.Version_id 
INNER JOIN Platform ON Solutions.Product_Platform = Platform.Platform_id 
INNER JOIN Issues ON Solutions.Product_Issue = Issues.Issue_id 
WHERE Product = '".$q."' AND Product_Issue = '".$p."'"; 




$result = mysqli_query($con,$sql); 


//below is the echo statment to create the results in a table format, list collumn titles 
echo "<table id=tables border='1'> 
<tr> 
<th>Products</th> 
<th>Version</th> 
<th>Platform</th> 
<th>Issue</th> 
<th>Sub Issue</th> 
<th>Fix</th> 



</tr>"; 

//below is script to list reults in a table format, $row [row name on table] 

while($row = mysqli_fetch_array($result)) 
{ 
echo "<tr>"; 
echo "<td>" . $row['Product_Name'] . "</td>"; 
echo "<td>" . $row['Version'] . "</td>"; 
echo "<td>" . $row['Platform_Name'] . "</td>"; 
echo "<td>" . $row['Issue'] . "</td>"; 
echo "<td>" . $row['Sub_Issue'] . "</td>"; 
echo "<td><a href=\"idfix.php?Fix=" . nl2br($row['Fix']) . "\">Fix</a></td>"; 
echo "</tr>"; 

} 

echo "</table>"; 

// below closes the coonection to mysql 


?> 
+0

我認爲問題出現在Javascript xmlhttp.open(「GET」,「getuser.php?p =」+ str,true); – Gustar

+0

你的意思是說,當你在查詢中刪除「AND」關鍵字時它就起作用了? – leonardeveloper

+0

我發現AND語句沒問題,這個問題可能會傳遞 – Gustar

回答

0

我認爲你需要Concat的下拉列表中的值。例如:

 <script type="text/javascript"> 
    function server(aform) 
    { 
    var xmlhttp; 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
    else 
     {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
    xmlhttp.open("GET","getuser.php?q="+ product.value +"&" +"p="+product_i.value ,true); 
    xmlhttp.send(); 
    } 
    </script> 
+0

信息它不起作用,不會引發任何的PHP – Gustar

相關問題