php - 根据选择的下拉菜单自动填充,需要帮助

标签 php javascript populate drop-down-menu

如何通过选择的下拉列表自动填充数据库中的数据? 我的下拉结果也已经出现,代码如下:

<?php
    echo '<tr>
    <td>'.$customer.'</td>
    <td><select name="customer_id">';

    foreach ($customers as $customer) {
        if ($customer['customer_id'] == $customer_id) {
            echo '<option value="'.$customer['customer_id'].'" selected="selected">'.$customer['name'].'</option>';
        } else {
            echo '<option value="'.$customer['customer_id'].'">'.$customer['name'].'</option>';
        }
    }
    echo '</select>
        </td>
    </tr>';
?>

上面的下拉列表的结果列出为

  • 管理员
  • 客户1
  • 免费

从以下数据库加载

INSERT INTO `my_customer` (`customer_id`, `name`, `firstname`, `lastname`) VALUES
(8, 'admin', '', ''),
(6, 'customer1', 'ok', ''),
(7, 'FREE', 'marj', 'om');

因此,每当选择下拉菜单时,我都需要以下所有数据:

<tr>
<td><?php echo $firstname; ?></td>
<td><?php echo $lastname; ?></td>
</tr>

也自动填充,似乎需要 javascript/ajax/jquery 来修复它,我想知道是否有人可以帮助我,提前感谢

<小时/>

添加 JSON 调用

我已经有 json 调用,如下所示: (假设它放置在 customer.php 中,网址为 index.php?p=page/customer)

public function customers() {
    $this->load->model('account/customer');
    if (isset($this->request->get['customer_id'])) {
        $customer_id = $this->request->get['customer_id'];
    } else {
        $customer_id = 0;
    }

    $customer_data = array();
    $results = $this->account_customer->getCustomer($customer_id);
    foreach ($results as $result) {
        $customer_data[] = array(
            'customer_id' => $result['customer_id'],
            'name'       => $result['name'],
            'firstname'       => $result['firstname'],
            'lastname'      => $result['lastname']
        );
    }

    $this->load->library('json');
    $this->response->setOutput(Json::encode($customer_data));
}

和数据库

public function getCustomer($customer_id) {
    $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
    return $query->row;
}

最佳答案

假设您使用的是 jQuery,您将监听选择更改事件,然后对 PHP 函数执行 ajax 调用以返回数据。然后将数据输出到适当的地方。我建议为下一个标签设置 id 属性:<select> , <td>对于名称,<td>对于姓氏,如下所示:

<select name="customer_id" id="customer_id>...</select>

<td id="firstname"> echo firstname </td>
<td id="lastname"> echo lastname </td>

然后是jquery代码:

<script type="text/javascript">//<!--
$(document).ready(function(){
    $('select#customer_id').change(function(){
        $.post(
            "http://www.domain.com/my_php_script.php",
            {customer_id: $(this).val()},
            function(data){
                $('td#firstname').html(data.firstname);
                $('td#lastname').html(data.lastname);
            }
        );
    });
});
//--></script>

假设您的my_php_script.php根据 $_POST['customer_id'] 中给定的 customer_id 从数据库检索数据并返回一个 JSON 对象,如 echo json_encode(array('firstname' => FIRSTNAME_FROM_QUERY, 'lastname' => LASTNAME_FROM_QUERY));

添加: 有两种解决方案 - 在 JS 中而不是

$.post()

你必须使用

$.get(...)

OR 在您的 PHP 脚本中代替

$this->request->get['customer_id']

你必须使用

$this->request->post['customer_id']

在每个地方...这应该可以... 例如:

<script type="text/javascript">//<!--
$(document).ready(function(){
    $('select#customer_id').change(function(){
        $.get(
            "http://www.domain.com/my_php_script.php",
            {customer_id: $(this).val()},
            function(data){
                $('td#firstname').html(data.firstname);
                $('td#lastname').html(data.lastname);
            }
        );
    });
});
//--></script>

关于php - 根据选择的下拉菜单自动填充,需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5883366/

相关文章:

php - 如何在不同时间将2条记录存储在同一列上?

php - 我该怎么做才能在我编辑此页面上的单元格时将其保存到数据库的表中?

javascript - 分别更改div的内容

javascript - 流动型类型转换

javascript - 在 JavaScript 中声明一个 const 有什么意义

javascript - 如何用变量定义一个数组,然后用一个数字填充每个数组?

php - Laravel 模型中的默认随机值

javascript - 除非单击提交按钮,否则离开页面警报

node.js - NodeJS + Mongoose : population doesnt work

node.js - 匹配 Mongoose 填充中的特定值