2016-12-29 61 views
0

這裏是我的代碼。如何獲得所選擇的價值組合框,並使用它的第二個組合框查詢

我有幾個組合框。我需要從ComboBox中選擇Value並在不同的查詢中使用值。我從Oracle選擇的所有數據。

這是我的第一個組合框,我需要使用這個組合框的選項的值在第二個組合框的查詢。我的意思是第二個ComboBox取決於第一個選擇。

我怎樣才能獲得「價值」?我試圖使用onchange="this.form.submit()",但它刷新頁面並丟失我的選擇。也許是有可能用Ajax做...

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

回答

0

我創造了新的PHP文件並將其命名爲 「js_post.php」。當我們從第一個ComboBox中選擇任何選項時,javascript將值發送到「js_post.php」,並且代碼正在檢查值並取決於值選擇選項。

<?php 
 
if(isset($_POST["action"]) && $_POST["action"]="get_data" && isset($_POST["muracietnov_id1"]) && $_POST["muracietnov_id1"] !="") 
 
{ 
 

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

 
\t } 
 
?>

這裏是Javascript和HTML,我用。

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

相關問題