从PHP获取数据以使用HTML

时间:2012-12-01 18:53:53

标签: php ajax

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

数据 servername联系分类评级 Server1 Ray Production A. Server2 Eric非生产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> 

servertest.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);      回声“Servername联系   分类评级“;
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); ?&GT;

1 个答案:

答案 0 :(得分: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>