简体   繁体   中英

How to get the selected value in ComboBox and use it for second ComboBox query

Here is my Code.

I have several ComboBoxes. I need to get Value from selected in ComboBox and use value in different query. All data I do select from Oracle.

This is my first ComboBox and I need to use VALUE of this ComboBox's option in query of second ComboBox. I mean second ComboBox depends on first select.

How I can get "Value"? I tried to use onchange="this.form.submit()" but it refreshes the page and loses my select. Maybe is it possible to do it with Ajax...

 <tr> <div class="form-group" style="background-color:#f9f9f9;border:1px solid #ddd;border-radius:4px;padding:2px;"> <label>Something:</label><span style="color:#FF0000;">*</span></label> <select class="form-control" input-sm name="example" required> <OPTION VALUE="">Choose any</OPTION> <OPTION VALUE="first">first_name</OPTION> <OPTION VALUE="second">second_name</OPTION> </select> </div> </tr> 

I created new php file and name it "js_post.php". When we chose any option from first ComboBox, javascript sending Value to "js_post.php" and there code is cheking Value and depends on Value selecting option.

 <?php if(isset($_POST["action"]) && $_POST["action"]="get_data" && isset($_POST["muracietnov_id1"]) && $_POST["muracietnov_id1"] !="") { if($_POST["muracietnov_id1"]==1) { //```````````````````````HERE SHould be your connection to DB```````````````` $select_query= sqlsrv_query($connection,$query); while($result=sqlsrv_fetch_array($select_query) { echo '<OPTION VALUE="'.$result['DATAID'].'">'.$result['NAME'].'</OPTION>'; } } ?> 

Here is Javascript and HTML that I used.

 $("#muracietnov_id").change(function(){ var url = 'js_post.php'; var muracietnov_id=$("#muracietnov_id").val(); var posting = $.post( url, { 'action': 'get_data','muracietnov_id1': muracietnov_id} ); posting.done(function( data ) { //alert(data); $("#muracietnov_id2").html(data); }); }); 
 <tr> <div class="form-group" style="background-color:#f9f9f9;border:1px solid #ddd;border-radius:4px;padding:2px;"> <label>Müraciət Növü:</label><span style="color:#FF0000;">*</span></label> <select class="form-control" input-sm name="muracietnov" id="muracietnov_id" required>'; <OPTION VALUE="">Seçiminizi edin</OPTION>'; <OPTION VALUE="1">Su təchizatı</OPTION>'; <OPTION VALUE="2">Kanalizasiya</OPTION>'; </select>'; </div> </tr> <tr> <div class="form-group" style="background-color:#f9f9f9;border:1px solid #ddd;border-radius:4px;padding:2px;"> <label>Müraciət Səbəbi:</label><span style="color:#FF0000;">*</span></label> <select class="form-control" input-sm id="muracietnov_id2" required> </select> </div> </tr> 

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