ALL EDITED
您好,
如何通过选择下拉列表自动填充db中的数据?我的下拉结果也已经出现,代码如下:
<?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; ?>
出现在
中<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上,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));
}
和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;
}
但是我得到了错误的回复,如下所示
有人请问如何更好地解决它?提前谢谢
答案 0 :(得分:0)
PHP编码风格的一些东西: 对于PHP代码块,不要为每一行使用新的php-Tags。此外,如果您想要PHP的HTML输出,可以使用echo-Method来执行此操作。所以你的代码看起来像这样:
<?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>';
?>
瘦,是每次PHP-Interpreter找到一个开放的PHP-Tag时,它开始解释它的代码,当它找到一个结束标记时,它就会停止这样做。因此,在您的代码中,解释器始终启动和停止。这不是很好的表现。
我想你想设置文本字段的值?这不是PHP的功能。这更像是一个JavaScript,因为它实际上发生在浏览器中,而不是在服务器上。
答案 1 :(得分:0)
而不是:
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'] + '" />');
}
}
在您的JS AJAX调用中执行此操作:
success: function(data) {
$('#show').append('<input type="text" name="customer_id" value="' + data['customer_id'] + '" /><input type="text" name="firstname" value="' + data['firstname'] + '" />');
}
因为您的PHP函数只返回一个对象。如果然后循环遍历对象属性你实际上循环遍历属性值的一个字符......