2014-11-25 62 views
0

我一直試圖自動填充使用js,php文件和窗體的表單字段,但它只顯示正確的行數,但我不能看到數據。在您檢查時請事先致謝;自動使用下拉填充文本框

這是我的表單被自動填充;

Reg No:  
    <select name="users" onchange="showVec(this.value)"> 
    <option value="">Select a RegNo:</option> 
    <?php 
     $Query = mysql_query("SELECT * FROM vehicledetails"); 
     while ($row = mysql_fetch_array($Query)) 
     { 
      $id = $row['id']; 
      $regno = $row['regno']; 
      $vehicletype = $row['vehicletype']; 
      $tankcapacity = $row['tankcapacity']; 
      $kml = $row['kml']; 
      $contingency = $row['contingency']; 
      $ccapacity = $row['ccapacity']; 
      echo "<option value=\"$id\">$regno</option>"; 
     } 
    ?> 
    </select> 
    <br><div id="GetInformation"> 

php文件是這樣的;

<?php 
    $q=$_GET["q"]; 
?> 

<?php 

    mysql_select_db("DropDown", $DBCONN); 

    $sql="SELECT * FROM vehicledetails WHERE id = '".$q."'"; 

    $result = mysql_query($sql); 

    if($result === FALSE) { 
     die(mysql_error()); // TODO: better error handling 
    } 

    while($row = mysql_fetch_array($result)) 
    { 
     $id = $row['id']; 
     $regno = $row['regno']; 
     $vehicletype = $row['vehicletype']; 
     $tankcapacity = $row['tankcapacity']; 
     $kml = $row['kml']; 
     $contingency = $row['contingency']; 
     $ccapacity = $row['ccapacity']; 

?> 
<p>Reg No: <input type="text" id="regno" name="regno" value="<?php echo $regno?>" ></p> 
<p>Vehicle Type <input type="text" id="vehicletype"name="vehicletype" value="<?php  echo $vehicletype?>"></p> 
<p>Tank Capacity <input type="text" id="tankcapacity" name="tankcapacity" value="<?php echo $tankcapacity?>"></p> 
<p>KM/Litre: <input type="text" id="kml" name="kml" value="<?php echo $kml?>" ></p> 
<p>Contingency: <input type="text" id="contingency" name="contingency" value="<?php echo $contingency?>"></p> 
<p>Carriage Capacity: <input type="text" id="ccapacity" name="ccapacity" value="<?php echo $ccapacity?>"></p> 

<?php 
    } 
    mysql_close($con); 
?> 

js腳本就是這樣的;

function showVec(str) 
{ 
    if (str=="") 
    { 
     document.getElementById("GetInformation").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("GetInformation").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","populatebudget.php?q="+str,true); 
xmlhttp.send(); 
} 
+0

你存儲的數據在哪裏? – 2014-11-25 07:04:32

+0

它在localhost中包含db.php在這個表單中@AkshayKhandelwal – 2014-11-25 07:13:50

+0

好吧,你可以將數據存儲在一些HTML元素上,最好是選擇像data-vehicletype那樣編寫id = $ regno的方式並通過jquery數據屬性特性讀取它。那麼你應該能夠在頁面上的任何位置填充數據 – 2014-11-25 07:22:29

回答

1

而不是mysql_fetch_array使用mysql_fetch_assoc,因爲我看到你是指同場名稱,而不是與索引查詢的結果。