PHP - 根据选择的下拉值自动填充文本框?

时间:2016-06-10 09:17:43

标签: php mysql

早上好,

我正在构建一个HTML / PHP的Web应用程序,并且有一个编辑产品页面,其中有一个选择下拉列表,该下拉列表存储在MySQL数据库中。以下表格的图像;

Edit product form

每个字段的相应数据也存储在数据库中,我无法解决我的生活如何通过下拉列表选择产品时如何获取文本框以显示相应的信息,我正在努力找到关于如何让它工作的任何教程?我知道我可能需要使用AJAX来获取信息而无需重新加载页面。为noob问题道歉我也是新的,

一如往常,任何帮助,

感谢。

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>';
    }
?>

3 个答案:

答案 0 :(得分: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

<?php
header('Content-Type: application/json');
$response = array(
    'ID' => null,
);

if (array_key_exists('product', $_POST)) {
    $product = $_POST['product'];

    // Now we fetch the product info from the database.
    // SELECT ID FROM product_info WHERE product = $product
    // Imagine the result is stored in $result
    $result = null;

    $response['ID'] = $result['ID'];
}

echo json_encode($response);

我没有意义为您提供能够立即解决问题的完整工作代码,但更希望向您展示解决这些问题的一般方法。

我希望这有帮助!

答案 1 :(得分:0)

尝试以下方法-

<p>
    <label for="inputState">State:</label>
    <select type="text" class="form-control show-tick" name="state" id="state" value="<?php echo $row['state']; ?>">
        <option value="0">Select State</option>
        <?php
            $sql = "SELECT * FROM states ";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    $name =  $row['name'];
                    $id =  $row['id'];
                    echo "<option value='$id'".(($state == $id)? 'selected' : '').">$name</option>";
                }
            } 
        ?>
    </select>
    <span class="error"><?php echo $stateErr; ?></span>
</p>

答案 2 :(得分:-1)

有两种方法可以通过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>
相关问题