php、html 组合框和文本字段

发布于 2024-10-15 06:02:59 字数 3555 浏览 3 评论 0原文

我制作了一个简单的应用程序,其中有一个加载了数据库值的组合框和一个应显示与组合框选择相关的文本的文本字段。 姓名 号码 号码1 1234 号码2 2345 号码2 5678 number3 2212

所以组合框将有值 number1、number2、number3。当用户选择 number1 时,我必须加载文本字段值 1234,如果选择 number2 2345,5678。

下面是我从数据库检索的代码...但问题是仅当我按 Enter 键而不是单击提交按钮时才加载该值...

<head>
    <title>Sample Numbers</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body >
    <?php
    $dbname = 'sample_db';
    $db_user = 'xxx';
    $db_pass = 'xxx';
    $host = 'localhost';
    $conn = mysql_connect($host, $db_user, $db_pass);
    mysql_select_db($dbname);
    $query = "select distinct Name from numbers";
    $result = mysql_query($query, $conn) or die(mysql_error());
    ?>
    <center>
        <form name=callsubm>
            <table>
                <?php
                if ($result) {
                ?>
                    <tr>
                        <td>Group Name:</td>
                        <td><select name="Name" id="Name" onchange="onComboChange();">
                        <?php
                            while ($row = mysql_fetch_assoc($result)) {
                                echo '<option value="' . $row['Name'] . '">' . $row['Name'] . '</option>';
                            }
                         }
                            ?>
                        </select>
                        <?php
                        if (isset($_GET['Name'])) {
                            $array = array();
                            $query = "select Number from numbers where Name='" . $_GET['Name'] . "'";
                            $result = mysql_query($query, $conn) or die(mysql_error());
                            $i = 0;
                            if ($result) {
                                while ($row = mysql_fetch_assoc($result)) {
                                    $array[] = $row['Number'];
                                    $i++;
                                }
                            }
                            $total_numbers = implode(',', $array);
                        }
                        ?>
                        <script type="text/javascript">
                            function showValues() {
                                var a=new Array();
           <?php
                                  for ($i = 0; $i < count($array); $i++) {
                                       echo "a[$i]='" . $array[$i] . "';\n";
                                  }
    ?>
                                  alert(a.join());
                                  document.getElementById("inTxt").value=a.join();
                            }
                        </script>
                    </td>
                </tr>
                <tr>
                    <td>Numbers :</td>
                    <td ><input name=inTxt id=inTxt type="text" size="15"></td>
                    <td><input type="button" id="callBtn" name="callBtn" value="submit" onclick="showValues()" ></td>
                </tr>
            </table>
        </form>
    </center>
    <script type="text/javascript">
        function onComboChange() {
         groupName=document.getElementById("Name").value;
         alert("Selected Group:" + groupName);
    }
    </script>
</body>

如何解决这个问题?

提前致谢

I made a simple application that have a combobox which is loaded with database value and a textfield which should display text related to combo box selection.
Name Number
number1 1234
number2 2345
number2 5678
number3 2212

So combo box will have values number1, number2, number3. when user selects number1, i have to load textfield value with 1234 and if number2 is selected 2345,5678.

Below is my code which retrieves from database ... but problem is the value is loaded only when i press enter key instead of clicking submit button...

<head>
    <title>Sample Numbers</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body >
    <?php
    $dbname = 'sample_db';
    $db_user = 'xxx';
    $db_pass = 'xxx';
    $host = 'localhost';
    $conn = mysql_connect($host, $db_user, $db_pass);
    mysql_select_db($dbname);
    $query = "select distinct Name from numbers";
    $result = mysql_query($query, $conn) or die(mysql_error());
    ?>
    <center>
        <form name=callsubm>
            <table>
                <?php
                if ($result) {
                ?>
                    <tr>
                        <td>Group Name:</td>
                        <td><select name="Name" id="Name" onchange="onComboChange();">
                        <?php
                            while ($row = mysql_fetch_assoc($result)) {
                                echo '<option value="' . $row['Name'] . '">' . $row['Name'] . '</option>';
                            }
                         }
                            ?>
                        </select>
                        <?php
                        if (isset($_GET['Name'])) {
                            $array = array();
                            $query = "select Number from numbers where Name='" . $_GET['Name'] . "'";
                            $result = mysql_query($query, $conn) or die(mysql_error());
                            $i = 0;
                            if ($result) {
                                while ($row = mysql_fetch_assoc($result)) {
                                    $array[] = $row['Number'];
                                    $i++;
                                }
                            }
                            $total_numbers = implode(',', $array);
                        }
                        ?>
                        <script type="text/javascript">
                            function showValues() {
                                var a=new Array();
           <?php
                                  for ($i = 0; $i < count($array); $i++) {
                                       echo "a[$i]='" . $array[$i] . "';\n";
                                  }
    ?>
                                  alert(a.join());
                                  document.getElementById("inTxt").value=a.join();
                            }
                        </script>
                    </td>
                </tr>
                <tr>
                    <td>Numbers :</td>
                    <td ><input name=inTxt id=inTxt type="text" size="15"></td>
                    <td><input type="button" id="callBtn" name="callBtn" value="submit" onclick="showValues()" ></td>
                </tr>
            </table>
        </form>
    </center>
    <script type="text/javascript">
        function onComboChange() {
         groupName=document.getElementById("Name").value;
         alert("Selected Group:" + groupName);
    }
    </script>
</body>

How to solve this?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-10-22 06:02:59

如果您需要提交按钮,但不一定是按钮,请使用

因此,将您的 html 更改为:(

<input type="submit" id="callBtn" name="callBtn" value="submit" onclick="showValues()" >

如果您不在 php 中查询该值,则可以省略 value="submit"...)

何时使用按钮:
如果您希望提交按钮上显示的文本与提交的值不同,请使用按钮:

<button type="submit" id="callBtn" name="callBtn" onclick="showValues()" 
    value="valueToBeSubmitted">textToBeDisplayed</button>

If you need a submit button, but it doesn't have to be a button, use an <input type="submit">.

Thus, change your html to:

<input type="submit" id="callBtn" name="callBtn" value="submit" onclick="showValues()" >

(You can omit the value="submit" if you don't query that value in php...)

When to use a button:
If you want the text being displayed on the submit button to be different to the value of the submit, you use a button:

<button type="submit" id="callBtn" name="callBtn" onclick="showValues()" 
    value="valueToBeSubmitted">textToBeDisplayed</button>
﹏雨一样淡蓝的深情 2024-10-22 06:02:59

更改要在提交按钮上提交的类型,而不是使用按钮作为类型:

<input type="submit" id="callBtn" name="callBtn" value="submit" onclick="showValues()" >

Change the type to submit on your submit button, rather than using button as the type:

<input type="submit" id="callBtn" name="callBtn" value="submit" onclick="showValues()" >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文