2017-11-11 67 views
1

當我在下拉菜單中選擇或選擇產品時,如何獲取我的product_rate的值並將其放入禁用的輸入字段中。獲取所選下拉菜單的數據

enter image description here

enter image description here

<?php 
    $con = mysqli_connect("localhost", "root", "", "dbselectedropdown"); 

    if (!$con) { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 
?> 
<html> 
<head> 
    <title>Selected Dropdown</title> 
</head> 
<body> 
    <select name="product_select" id="product_select"> 
     <option>--SELECT--</option> 
     <?php 
      $query = mysqli_query($con, "SELECT * FROM tbselectedropdown"); 
      while ($row = mysqli_fetch_array($query)) { 
       echo "<option>" .$row["product_name"]. "</option>"; 
      } 
     ?> 
    </select><br/><br/><br/><br/><br/><br/> 
    <input type="text" name="product_inputfield" id="product_inputfield" disabled/> 
</body> 
<script> 

</script> 
</html> 

回答

1

一是提高PHP:

$query = mysqli_query($con, "SELECT * FROM tbselectedropdown"); 
while ($row = mysqli_fetch_array($query)) { 
    echo "<option value=" . $row["product_rate"] . ">" .$row["product_name"]. "</option>"; 
} 

二,添加腳本:

<script> 
    var input = document.getElementById('product_select') 
    input.addEventListener('change', function (e) { 
    document.getElementById('product_inputfield').value = e.value 
    }) 
</script> 

或者,如果你正在使用jQuery :)

<script> 
    $('#product_select').change(function() { 
    $('product_inputfield').val($(this).val()) 
    }) 
</script> 
+0

解決了!非常感謝你。我不知道可以提取數據並將其放入值中。哈哈。 –

+0

@FrancisJohnVargas這是確定:)簡單的,你可以看到:) – WaldemarIce

+0

是的,雖然我已經在那裏使用jQuery。哈哈。順便說一句,我不能從現在開始喜歡你的答案。它說10分鐘後,我真的很感謝你的幫助。再次感謝你。 –

0

如果您已經在使用,我建議您使用JQuery,這相當簡單。聆聽任何更改,如果有更改,則將文本添加到禁用的輸入。

// create new function on change of select input 
 
$('#product_select').change(function() { 
 
    // create variable for product 
 
    var product = $("#product_select").find(':selected').text(); 
 
    // create an input variable 
 
    var input = $("#product_inputfield"); 
 
    
 
    // for testing you can output the select 
 
    alert('Changed value: ' + product); 
 
    
 
    // create the if statements 
 
    if(product === 'acer') { 
 
    input.val(1500); // change the value with your PHP 
 
    // <?=$row['product_rate'];?> 
 
    } else if(product === 'samsung') { 
 
    input.val(3200); 
 
    } else if(product === 'lenovo') { 
 
    input.val(5300); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
<head> 
 
    <title>Selected Dropdown</title> 
 
</head> 
 
<body> 
 
    <select name="product_select" id="product_select"> 
 
     <option value="">--SELECT--</option> 
 
     <option value="acer">acer</option> 
 
     <option value="samsung">samsung</option> 
 
     <option value="lenovo">lenovo</option> 
 
    </select><br/><br/><br/><br/><br/><br/> 
 
    <input type="text" name="product_inputfield" id="product_inputfield" readonly/> 
 
</body> 
 
<script> 
 

 
</script> 
 
</html>