2016-06-10 172 views
0

早上所有,PHP - 根據選擇的下拉值自動填充文本框?

我正在用HTML/PHP構建一個web應用程序,並且有一個編輯產品頁面,其中有一個從存儲在MySQL數據庫中的產品填充的選擇下拉列表。下面的表格的圖像;

Edit product form

每個字段的相應數據也存儲在數據庫中,我不能爲我的生活中,我是如何得到的文本框,它能夠實現相應的信息時,通過下拉菜單選擇產品摸出,並且我正在努力尋找關於我如何能夠完成這項工作的任何教程?我明白,我可能需要使用AJAX來獲取信息,而無需重新加載頁面。對於小白quesitons道歉,我是新太本,

與往常一樣,任何幫助appriciated,

感謝。

PHP到目前爲止其填充選擇下拉菜單:

<?php 


    or die ('Cannot connect to db'); 

$result = $conn->query("select ID, NAME from PRODUCTS"); 

print "<h3>EDIT PRODUCT</h3>"; 

print "<p>&nbsp;<strong>SELECT PRODUCT: <br><br></strong>" . "<select name='ID'"; 

while ($row = $result->fetch_assoc()) { 

       unset($id, $name); 
       $id = $row['ID']; 
       $name = $row['NAME']; 
       echo '<option value="'.$id.'">'.$name.'</option>'; 

} 

?> 

回答

0

你需要使用一些JavaScript!看看以下...我沒有測試過,所以你可能會遇到一個錯誤,但它應該很容易修復。

我只添加了一些代碼,在選擇框中選擇產品後預先填充單個文本框,但您應該能夠找出其餘的。

<?php 

$conn = new mysqli('localhost', 'bradleyb_badbrad', '20password40', 'bradleyb_testDb') 
or die ('Cannot connect to db'); 

$result = $conn->query("select ID, NAME from PRODUCTS"); 

// Build up an array of options to show in our select box. 
$productSelectOptions = array(); 
while ($row = $result->fetch_assoc()) { 
    $productSelectOptions[$row['ID']] = $row['NAME']; 
} 

?> 

<h3>EDIT PRODUCT</h3> 

<p> 
    <strong>SELECT PRODUCT:</strong> 
    <select name="ID" id="productSelect"> 
     <?php foreach ($productSelectOptions as $val => $text): ?> 
      <option value="<?= $val; ?>"><?= $text; ?></option> 
     <?php endforeach; ?> 
    </select> 
</p> 

<h3>ID:</h3> 

<p> 
    <input type="text" name="id_text" id="idText" /> 
</p> 

<!-- Include jQuery --> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 

<script> 
    var $productSelect = $('#productSelect'); 
    var $idText = $('#idText'); 

    // This should be the path to a PHP script set up to receive $_POST['product'] 
    // and return the product info in a JSON encoded array. 
    // You should also set the Content-Type of the response to application/json so as our javascript parses it automatically. 
    var apiUrl = '/getProductInfo.php'; 

    function refreshInputsForProduct(product) 
    { 
     $.post(apiUrl, {product: product}, function(r) { 
      /** 
      * Assuming your PHP API responds with a JSON encoded array, with the ID available. 
      */ 
      $idText.val(r.ID); 
     }); 
    } 

    // Listen for when a new product is selected. 
    $productSelect.change(function() { 
     var product = $(this).val(); 
     refreshInputsForProduct(product); 
    }); 
</script> 

例如PHP API一些一般性說明... /getProductInfo.php

​​

我沒有意思,給你充分的工作代碼,立即修復您的問題,但更希望向您展示關於這些事情的一般方法。

我希望這有助於!

+0

真的很感謝!我想我會得到它..將有一個遊戲周圍,讓你知道如何去。再次感謝! – boothprod

0

有兩種方法可以通過AJAX或通過提交表單解決此問題。 通過php表單提交示例。

<?php 
//after submitting the form you get the value of selected product 
if(isset($_GET['id'])){ 
    $product_id=$_GET['ID']; 
    $productQuery = $conn->query("select ID, NAME from PRODUCTS WHERE ID='".$."'"); 
    $productResult = $productQuery ->fetch_assoc() 
    //use the value of productResult in your textbooxes 
}else{ 
    $productResult =array(); 
} 
<form method='get' action=''> 
    <select name='ID' onchange="this.form.submit()"> 
<?php 
    while ($row = $result->fetch_assoc()) { 
     unset($id, $name); 
     $id = $row['ID']; 
     $name = $row['NAME']; 
     echo '<option value="'.$id.'">'.$name.'</option>'; 
    }?> 
</select> 
</form> 
相關問題