2014-11-25 61 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,因为我看到你是指同场名称,而不是与索引查询的结果。