0

我尝试了很多事情,但没有一个成功。我试图在选择框(选择以提取资产中心数据)后更改评级选项。它获取 php 文件中的数据,但我不知道如何处理 html 文件中的数据。

数据 servername 联系人分类等级 Server1 Ray Production A Server2 Eric Non-Production B

--

-- 数据库:ipreservation


--

-- 表的表结构acdata

CREATE TABLE IF NOT EXISTS `acdata` (
  `servername` varchar(25) NOT NULL,
  `contact` varchar(20) NOT NULL,
  `classification` varchar(15) NOT NULL,
  `rating` varchar(4) NOT NULL 
) 
ENGINE=InnoDB DEFAULT
CHARSET=latin1;

--

-- 为表转储数据acdata

INSERT INTO `acdata` (`servername`, `contact`, `classification`,`rating`) 
VALUES ('Server1', 'Ray', 'Production', 'A'),
('Server2','Eric', 'Non-Production', 'B');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /; / !40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /; / !40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

servertest.html

<html>
<head>
<script>
function showServer(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;
    }
  }
var server = document.getElementById('existingserver').value;
xmlhttp.open("GET","servertest.php?q="+server,true);
xmlhttp.send();

}
</script>
</head>
<body>

<form>
<label>Existing Server</label><input type="text" name="existingserver" id="textfield" maxlength="15"/>
<input type="checkbox" onclick="showServer(this.value)" >Select to Pull Asset Center Data<br>
<br />
<br />
Rating
<select name="rating" id="rating">
  <option value="A">A</option>
  <option value="B">B</option>
  <option value="C">C</option>
  <option value="D">D</option>
</select>
</form>
<br />

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

</body>
</html> 

服务器测试.php

$con = mysql_connect('localhost', 'assignip', 'assignip'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ipreservation", $con); $sql = "SELECT * FROM acdata WHERE servername = '".$q."'"; $result = mysql_query($sql); echo "服务器名联系人分类等级";
while($row = mysql_fetch_array($result))   {
    echo "<tr>";
        echo "<td>" . $row['servername'] . "</td>";
        echo "<td>" . $row['contact']. "</td>";
        echo "<td>" . $row['classification'] . "</td>";
        echo "<td>" . $row['rating'] . "</td>";
    echo "</tr>";   
} 
echo "</table>";
mysql_close($con); ?>
4

1 回答 1

0

您的代码使用 jQuery 简化并可以正常工作,请在下面找到您更新的代码

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script type="text/javascript" >
        $(document).ready(function(){
            var id = $('#existingserver').val();            
            $('#assetCenter').click(function(){
                var id = $('#textfield').val();
                $.get('servertest.php',{q:id}, function(htmlData){
                    $('#txtHint').html(htmlData);                    
                    $('#rating').val(htmlData); 
                });
            });
        });    
        </script>
    </head>
    <body>

        <form>
            <label>Existing Server</label><input type="text" name="existingserver" id="textfield" maxlength="15"/>

            <input type="checkbox" id="assetCenter" onclick="showServer(this.value)" >Select to Pull Asset Center Data<br>
            <br />
            <br />
            Rating
            <select name="rating" id="rating">
                <option value="A">A</option>
                <option value="B">B</option>
                <option value="C">C</option>
                <option value="D">D</option>
            </select>
        </form>
        <br />

        <div id="txtHint"><b>Server info will be listed here.</b></div>
    </body>
</html> 
于 2012-12-01T19:13:14.217 回答