所有已编辑
嗨,
我怎么能自动从数据库中通过下拉菜单选择填充数据? 和我的下拉结果已经出现为好,代码如下:
<?php
echo '<tr>
<td>'.$customer_data.'</td>
<td><select name="customer_id" id="customer_id" onchange="getCustomer();">';
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>';
?>
具有HTML视图代码作为
<select name="customer_id" id="customer_id" onchange="getCustomer();">
<option value="8">admin</option>
<option value="6">customer1</option>
<option value="7" selected="selected">FREE</option>
</select>
现在如果选择下拉菜单中的一个,我想其他如<?php echo $firstname; ?>, <?php echo $lastname; ?>
<?php echo $firstname; ?>, <?php echo $lastname; ?>
出现在
<tr>
<td><div id="show"></div></td>
</tr>
基于客户ID /选择的名称
这样做,我尝试使用JSON调用如下:
<script type="text/javascript"><!--
function getCustomer() {
$('#show input').remove();
$.ajax({
url: 'index.php?p=customer/customers&customer_id=' + $('#customer_id').attr('value'),
dataType: 'json',
success: function(data) {
for (i = 0; i < data.length; i++) {
$('#show').append('<input type="text" name="customer_id" value="' + data[i]['customer_id'] + '" /><input type="text" name="firstname" value="' + data[i]['firstname'] + '" />');
}
}
});
}
getCustomer();
//--></script>
PHP的通话JSON放置在customer.php与网址的index.php?p =页/客户)
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;
}
但我得到错误的返回如下
有没有人请如何解决它更多更好? 提前致谢