0

当用户选择下拉列表值时,价格将根据数据库显示。我正在考虑代码。我希望任何人都可以在这里帮助我。

<form action="next.php" method="post">

<center>

<table>

用于显示表中的数据库行名的下拉列表 tabelmedicine

<tr><td width="116">Medicine name</td><td width="221">

<center>:

<select name="name" id="name">

<option>--- Choose Medicine ---</option>

与数据库连接

<?php

mysql_connect("localhost", "root", "");

mysql_select_db("arie");


$sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");

if(mysql_num_rows($sql) != 0){

while($row = mysql_fetch_assoc($sql)){

echo '<option>'.$row['name'].'</option>';


}

}

?>

</select ></center>

</p></td></tr>

文本字段显示来自 tabelmedicine 行 priceperunit 的价格值

<tr><td><p>Price</p></td><td><p align="center">:<input type="text" name="price" 

id="price"value="<?php echo ('priceperunit'); ?>" onClick="checkprice()">

</p></td></tr>


<script>

var select = document.getElementById('name');

var input = document.getElementById('price');

select.onchange = function()

{

    input.value = select.value;

}

</script>
4

1 回答 1

1

这应该这样做。只要确保$row['price']是正确的数据库字段键

<form action="insertout.php" method="post">
<center>
<table>
<tr>
    <td width="116">Medicine name</td>
    <td width="221">
        <center>
            :
            <select name="name" id="name">
                <option>--- Choose Medicine ---</option>
                <?php
                    mysql_connect("localhost", "root", "");
                    mysql_select_db("arie");
                    $sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");
                    if(mysql_num_rows($sql) != 0){
                        while($row = mysql_fetch_assoc($sql)){
                            echo '<option value="'.$row['price'].'">'.$row['name'].'</option>';
                        }
                    }
                ?>
            </select >
        </center>
        </p>
    </td>
</tr>
<tr>
    <td>
        <p>Price</p>
    </td>
    <td>
        <p align="center">:<input type="text" name="price" 
            id="price"value="<?php echo ('priceperunit'); ?>" onClick="checkprice()">
        </p>
    </td>
</tr>
<script>
    var select = document.getElementById('name');
    var input = document.getElementById('price');
    select.onchange = function(){
        input.value = select.value;
    }
</script>
于 2013-09-13T15:34:03.873 回答