【发布时间】:2011-05-04 12:28:39
【问题描述】:
如何通过选择的下拉菜单自动填充 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>';
?>
上面下拉列表的结果为
- 管理员
- 客户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 CALL
我已经有如下的 json 调用: (假设这个放在 customer.php 中,url 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;
}
【问题讨论】:
-
您到底想做什么...尝试重新表述您的问题,以便人们能够正确理解它。
标签: php javascript populate drop-down-menu