简体   繁体   中英

php, mysql, jquery, form element chain select

I'm having trouble with a chained (form element) select. I can't seem to trigger with jquery the second select options to be populated with the select using $examplevar as a filter resulting from the option selected in the first dropdown list.

    <form name="vlan" action="" method="post"> 
      <div class="row">
        <div class="col-md-6">
        <?php
        $con_string= "host=example port=xxxx dbname=example user=example password=example";
        $db_con = pg_connect($con_string);
        $dev = pg_query($db_con, "SELECT DISTINCT \"Type\" FROM \"End_Devices\"");

                            echo "  
                            <a class=\"current_page_item\">Location NASID:</a>
                            <input type=\"text\" name=\"nasid\" />      
                            <br>
                            <a class=\"current_page_item\">Device Name:</a>
                            <input type=\"text\" name=\"name\" />   
                            <br>
                            <input type=\"hidden\" name=\"dvtype\"/>
                            <a class=\"current_page_item\">Device Type:</a> 
                            <select name=\"slType\" id=\"slType\">";

                            while ($device = pg_fetch_array($dev))
                            {
                                echo "<option id=\"" .$device['Type'].  "\">" .$device['Type']. "</option>";

                            }
                            echo "<script type=\"text/javascript\">

                                $(\"#slType\").chained(\"#slModel\");
                                $(\"#slType\").on(\"change\",function(){
                                    var slTypeID = $(this).val();
                                    $.post('inventoryTEST.php', {slTypeID:slTypeID}, function(data){
                                        $('#slModel').html(data);
                                    });
                                });

                            </script>";


                            echo "
                            </select>
                            <br>
                            <a class=\"current_page_item\">Device Model:</a>
                            <select  name=\"slModel\" id=\"slModel\" >";

                            if (isset($_POST['slTypeid']))
                            {
                                $selection = ($_POST(['slTypeid']));

                            }

                            $mod = pg_query($db_con, "SELECT \"Model\" FROM \"End_Devices\" WHERE \"Type\"=\"". $selection . "\"");
                            while ($model = pg_fetch_array($mod))
                            {
                                echo $_POST[type];
                                echo "<option id=\"" .$model['Model'].  "\">" .$model['Model']. "</option>";
                            }
                            echo "
                            </select>
                            <br>
                            <a class=\"current_page_item\">Physical Location:</a>
                            <input type=\"text\" name=\"location\" />
                            <br>
                            <a class=\"current_page_item\">Serial:</a>
                            <input type=\"text\" name=\"serial\" /> 
                            <br>
                            <a class=\"current_page_item\">IP Address:</a>
                            <input type=\"text\" name=\"ip\" />
                            <br>
                            <a class=\"current_page_item\">MAC:</a>
                            <input type=\"text\" name=\"mac\" />    
                            <br>
                            <a class=\"current_page_item\">SSID:</a>
                            <input type=\"text\" name=\"ssid\" />
                            <br>
                            <a class=\"current_page_item\">WLAN Area:</a>
                            <input type=\"text\" name=\"wlan\" />   
                            <br>
                            <br>
                            <br>
                        ";
                        ?>
                            <!--SUBMIT INPUT-->
                            <input type="submit" value="Save" name="submit" />
                        </div>
                    </div>
                </form>

Can anyone please advise on the code above? I'm not able to trigger the on change event to populate the var which is used as filter on the sql query, to then populate the second chained dropdown. thanks Best regards,

通过使用jquery和.ajax来解决此问题,方法是将选定的行内容.change与post一起传递到外部php,然后在其中查询db,将其用作过滤器并填充第二个组合框。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM