duanfeng7756 2014-11-25 07:01
浏览 115
已采纳

使用下拉菜单自动填充文本框

I have been trying to auto populate form fields using js, php file and form but it only display the correct numbers of rows but i cant see data. Thanks in advance as you check this;

this is my form to be auto filled;

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">

The php file is like this;

<?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);
?> 

the js script is like this;

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();
}
  • 写回答

1条回答 默认 最新

  • dtz30833 2014-11-25 07:26
    关注

    Instead of mysql_fetch_array use mysql_fetch_assoc, as i see that you are referring the result of the query with the field name and not with the index.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办