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

I need to see showValues() js function

I have solved this issue using ajax request - response. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.