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

时间:2014-11-25 07:01:39

标签: javascript php jquery

我一直在尝试使用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();
}

1 个答案:

答案 0 :(得分:1)

而不是 mysql_fetch_array 使用 mysql_fetch_assoc ,因为我看到您使用字段名称而不是使用索引。