2012-09-27 82 views
0

填充雖然我敢肯定,這一定有什麼真的很明顯,我看不到我要去的地方錯。我有一個下拉列表,其中有兩個選項。當我選擇一個選項時,它應該使用XMLHttpRequest()根據選擇的選項從數據庫中獲取客戶列表。下拉列表中不使用Ajax

我有兩個部分:

ajax2_js.php - 包含JavaScript和HTML的形式。

ajax2_DBAccess.php - 包含從DATABSE獲取列表中的PHP。

我已經檢查了第二頁上的所有內容,並能正常工作在它自己的(並顯示相關列表作爲下拉菜單),但是當我選擇第一頁上的選項,沒有任何反應。

我的代碼到目前爲止是:

ajax2_js.php

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
     <script> 
function ajaxFunction() 
{ 
var ajaxRequest; 
ajaxRequest = new XMLHttpRequest(); 

ajaxRequest.onreadystatechange = function() 
    { 
     if(ajaxRequest.readyState == 4) 
      { 
       document.getElementById('customerDiv').innerHTML=req.responseText; 
      } 
    } 
ajaxResuest.open("GET", strURL, true); 
ajaxRequest.send(null); 
} 
     </script>   
</head> 

<body> 
    <form method="post" action="" name="form1"> 
     Network : <select name="network" onChange="ajaxFunction('ajax2_DBAccess.php?network='+this.value)"> 
      <option value="">Select Network</option> 
      <option value="1">Net1</option> 
      <option value="2">Net2</option> 
     </select> 
     </br> 
     Customer : <div id="customerDiv"> 
      <select name="select"> 
       <option>Select Customer</option> 
      </select> 
     </div> 
    </form> 
</body> 

ajax2_DBAccess.php

<?php 
$network=$_GET['network']; 
$q1 = "SELECT `CustName` FROM monitor.customers where network = $network;"; 

$con = new mysqli('localhost:3306', 'xxx', 'xxx'); 

if (mysqli_connect_errno()) 
    { 
     $error = mysqli_connect_error(); 
     echo $error; 
     exit(); 
    } 
else 
    { 
     $ConfRes = mysqli_query($con, $q1); 
     if ($ConfRes) 
      { 
       echo "<select name=\"Customers\">"; 
       echo "<option>Select Customer</option>"; 
       while($row=mysqli_fetch_array($ConfRes, MYSQLI_ASSOC)) 
        { 
         $result = $row['CustName']; 
         echo "<option value>$result</option>"; 
        }; 
       echo "</select>"; 
      } 
     else 
      { 
       $error = mysqli_error(); 
       echo $error; 
       exit(); 
      } 
    }; 
?> 

任何援助將不勝感激。

+0

'ajaxFunction'中'strURL'的值是什麼? – wroniasty

回答

0

檢查JavaScript錯誤日誌。 這可能是問題,「請求」中的拼寫錯誤。 ajaxResuest.open(「GET」,strURL,true);

而且,您的SQL查詢從$網絡參數的SQL注入漏洞受到影響。

0

您可以使用XML或JSON返回列表。這tutorial應該幫助。我個人會使用XML。

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each 

while ($row = @mysql_fetch_assoc($result)){ 
    // ADD TO XML DOCUMENT NODE 
    $node = $dom->createElement("marker"); 
    $newnode = $parnode->appendChild($node); 
    $newnode->setAttribute("CustName",$row['CustName']); 
    } 

echo $dom->saveXML(); 

但是有很多關於這兩種方法的教程。

0

感謝您的所有幫助球員,我已經跟蹤它到三件事(都是我的錯):

function ajaxFunction() 

應該是:

function ajaxFunction(strURL) 

ajaxResuest.open("GET", strURL, true); 

應該是:

ajaxRequest.open("GET", strURL, true); 

最後:

document.getElementById('customerDiv').innerHTML=req.responseText; 

應該

document.getElementById('customerDiv').innerHTML=ajaxRequest.responseText; 

(當然還有上面提到的SQL注入漏洞,我也會修復)。

歡呼聲。