2015-08-24 17 views
0

我在主頁面有兩個下拉列表,當用戶點擊國家下拉列表(在更改時),另一個下拉列表將是與產品列表一起顯示。我從國家下拉列表中使用ajax調用(getproduts.php)完成。屏幕截圖,ajax調用和程序被附上。如何從一個下拉框中獲取多個字段在其他程序中使用

下拉列表效果很好,我也可以選擇該項目。下拉列表中包含產品說明和價格。

我想在用戶選擇該選項時在主程序中使用變量的價格。

當用戶選擇產品時,主程序中的子總價值(附加屏幕)應該隨下拉框中的價格而變化。 我該如何做到這一點?

Ajax call 

<script type="text/javascript"> 
function get_products()`enter code here` 
    { 
      var country = $("#country").val(); 
    var type = $("#type").val(); 
    var dataString = 'country='+ country + '&type=' + type; 

    $.ajax({ 
     type: "POST",`enter code here` 
     url: "getproducts.php", 
     data: dataString, 
     success: function(html) 
     { 
     $("#get_products").html(html); 
     } 
    }); 
} 

</script> 


getproducts.php 

<?php 
error_reporting(1); 
//print_r($_POST); 
include('function/db_connect.php'); 
//session_start(); 
$price = Null; 
//$items = Null; 
if($_POST) 
{ 
$var1 = $_POST['country']; 
$var2 = $_POST['type']; 
if ($var1 != '') {   
     echo "<label>Item<span style='color:red'>*</span></label><span style='color:red'></span></label><span class='address'>"; 
     echo "<select id='items' name='items' style = 'width: 546px;' onChange='get_price(this.value)'>";   
     $sql = "SELECT * FROM tbl_product WHERE country_id = '$var1' AND type_id = '$var2'"; 
     $db = new DB_CONNECT();  
     $result = mysql_query($sql); 
     $myarray = array(); 
     echo "<option value=''>Select</option>"; 
     while ($row = mysql_fetch_array($result)) { 
      $idp = $row["product_id"]; 
      $iddes = $row["product_desc"]; 
      $selp = $row["product_sell"]; 
      $costp = $row["product_cost"]; 


     echo "<option value='" . $idp . "'>" . $iddes . "==>".$selp ."</option>"; 

     } 
     echo "</select><label>Item</label></span><span class='address'>"; 

    } 
     echo "</div>";   
     echo "<br>";  



     echo "<div><label></label><span class='name'><button name = 'data' type ='button' onclick= 'valprice('items')'>Validate value</button></span></div>";  

    } 
?> 
+0

哪裏是你'get_price'功能? –

+0

這是要選擇產品的價格,我用這個作爲替代,因爲我不知道如何從同一個表中選擇多個字段並存儲在某個變量中,並在用戶選擇prducts – kamalg

+0

有人能幫助我嗎? – kamalg

回答

0

現在在你的選擇HTML元素,你使用onChange='get_price(this.value),這行代碼只得到在這種情況下,只有$idp = $row["product_id"];期權的價值。如果您想選擇其他字段,則需要在發生循環過程中將這些值附加到選項本身。以下是示例用法。

在您選擇的標籤,變成這樣:

echo "<option value='" . $idp . "' data-price='" . $selp . "'>" . $iddes . "==>".$selp ."</option>"; 

你看,我加入data-price='" . $selp . "'作爲一個用戶定義的屬性。這在稍後我們取onchange事件。

之後,平變化改變你的選擇HTML元素插入此:

echo "<select id='items' name='items' style = 'width: 546px;' onChange='get_price(this)'>"; 

最後,在get_price()功能調整到這個下面的代碼:

<script> 
// array variable to store product details[id and price in this case] 
var myProduct = []; 
function getPrice(e) { 
    // set to empty for initial 
    myProduct.length = 0; 
    // fetch price 
    var price = $('option:selected', $(e)).data('price'); 
    // fetch val ID 
    var valID = $(e).val(); 

    // insert into array variable as a javascript object 
    myProduct.push({ 
    ID : valID, 
    price : price 
    }); 

    console.log(myProduct); 

} 
</script> 

要提取其他領域,把另一data-attributes像上面的例子。

DEMO - 檢查控制檯查看輸出

相關問題