2

mySQL データベースからデバイスのリストを取得しようとしています。これを .php の html select/option 要素にフォーマットし、html の文字列をエコーし​​てメイン ページに挿入しますが、そうはなりません。エコーした文字列を responseText に返します。

ここに私の.phpがあります:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "admin";
$dbname = "devices";
    //Connect to MySQL Server
$con=mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
mysql_select_db($dbname) or die(mysql_error());

$sql="SELECT DISTINCT device_name FROM device_graphs";
$result=mysql_query($sql, $con);

$responsetxt="Hello all :)";
$counter=0;
while($row = mysql_fetch_array($result))
{
    $responsetxt .= "<option value=$counter>$row[0]</option>";
    $counter++;
}

echo $responsetxt;

?>

データベースにリストされているデバイスに基づいて、デバイス名のドロップダウン リストを生成しようとしています。ブラウザで .php ファイルだけを開くと、正しくエコーされます。

function deviceDropdown(monNum, insideHTML)
{
var ajaxRequest;  // The variable that makes Ajax possible! 
try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        alert(ajaxRequest.responseText);
        ajaxresponse = ajaxRequest.responseText;
        alert(ajaxresponse);
        deviceDropdown_pt2(monNum, ajaxresponse, insideHTML);
    }
}
ajaxRequest.open("GET", "device_list.php", true);
ajaxRequest.send(null); 
}

これを試すたびに ajaxresponse は未定義です。2 つの空の JavaScript アラート ボックスが表示されるだけです。ブラウザで .php だけを実行すると、ウィンドウに正しい出力がエコーされるので、php 経由で ajax を介して html を渡す際に問題がない限り、php に問題があるとは思いません。

ドロップダウン コードの残りの部分は次のとおりです。

function deviceDropdown_pt2(monNum, ajaxresponse, insideHTML)
{
//Sets the action to occur when a selection is made in the device dropdown, 
//  in this case the properties dropdown menu
insideHTML=insideHTML + "<select onchange='propertiesDropdown(this.options[this.selectedIndex].value, " +  monNum + ")'>";  
insideHTML=insideHTML + "<option value=\"-1\">-Select a Device-</option>";
insideHTML = insideHTML + ajaxresponse; 
insideHTML=insideHTML + "</select>";
}

私のメインの.jsには、次のものが定義されていますvar iframe=document.getElementById("bdy");

insideHTML="<table id=\"dashboard\" align=\"left\" valign=\"top\" border=\"0\">";
insideHTML=insideHTML + "<tr>";
insideHTML=insideHTML + "<td id=\"menu1\" width=\"800\">";
deviceDropdown("1", insideHTML);
insideHTML=insideHTML + "</td></tr>"; 
iframe.innerHTML=insideHTML;
4

1 に答える 1

0

http://jquery.com/を使用する必要があります

達成したいことの簡単な例:

$.get('ajax/test.html', function(data) {
    $('.result').html(data);
    alert('Load was performed.');
});

そして、あなたがしなければならないのはあなたのヘッダーにこれを含めることです:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script
于 2011-08-03T08:58:27.173 に答える