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

时间:2011-05-04 12:28:39

标签: php javascript populate drop-down-menu

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

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

以及下面列出的结果列为

  • 管理员
  • customer1表

从以下db

加载
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 CALL

我已经将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));
}

和db

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

1 个答案:

答案 0 :(得分:0)

假设您正在使用jQuery,您将监听select change事件,然后对将返回数据的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));

<强> ADDITION : 如何解决它有两种选择 - 在JS而不是

$.post()

你必须使用

$.get(...)
PHP脚本中的

OR 而不是

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

你必须使用

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

在每个地方......这应该做到...... E.g:

<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>