2012-11-10 46 views
2

這裏有一個例子,如果我輸入識別碼,然後它會從數據庫中檢索數據表單字段內&打印。我的數據庫只有三個列ID,名稱&號碼。這裏是我的index.html文件:檢索SQL數據和其打印成表單字段

<html> 

<head> 

    <script> 

     function showData(str) 
     { 
      if (str=="") 
      { 
       document.getElementById("ajax-content").innerHTML=""; 
       return; 
      } 

      // Code for IE7+, Firefox, Chrome, Opera, Safari 
      if (window.XMLHttpRequest) 
      { 
       xmlhttp=new XMLHttpRequest(); 
      } 

      // Code for IE6, IE5 
      else 
      { 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 

      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        document.getElementById("ajax-content").innerHTML=xmlhttp.responseText; 
       } 
      } 

      xmlhttp.open("GET","showData.php?id="+str,true); 

      xmlhttp.send(); 

     } 
     return true; 

    </script> 

</head> 

<body> 


    <p>Please Enter Your Staff ID:</p> 
    <div><input onkeyup="if (event.keyCode == 13) showData(this.value); return false;" /></div> 



<div id="ajax-content"></div> 

</body> 
</html> 

,這裏是我的showData.php文件:

<?php 

    // Receive variable from URI 
    $id=$_GET["id"]; 

    // Connect to your database 
    $con = mysql_connect('localhost', 'root', ''); 
    if (!$con) 
    { 
     die('Could not connect: ' . mysql_error()); 
    } 

    // Select your database 
    mysql_select_db("test_01", $con); 

    // Select all fields from your table 
    $sql="SELECT * FROM staffdetails WHERE id = '".$id."'"; 

    $result = mysql_query($sql); 

    while($row = mysql_fetch_array($result)) 
    { 

     echo "Staff Name:" . "<input type='text' value='" . $row['name'] . "'>"; 
     echo "</br>"; 
     echo "Contact No.:" . "<input type='text' value='" . $row['number'] . "'>"; 
    } 

    // Close the connection 
    mysql_close($con); 

?> 

現在我想要做的是,在給定的表單字段顯示的數據,而比使用表單域打印數據。這意味着,名稱&的數字輸入字段將已經以id字段的形式出現。當我輸入id時,它會將數據回退&將其顯示在給定的表單字段中。任何人都可以幫忙嗎?謝謝!

回答

0

有你index.html頁面JavaScript錯誤。 return true陳述應位於功能括號內。

也可以嘗試在index.html頁面的頂部添加文檔類型內容類型顯示文本字段,而不是打印出來的。

希望這將有助於解決您的問題。