使用下拉列表自动填充数据 [英] auto populate data with dropdown

查看:318
本文介绍了使用下拉列表自动填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ALL EDITED

您好,

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

How can I auto populate the data from db by dropdown selected? and my dropdown result already appear as well, the code as following:

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

now if one of dropdown selected i want another e.g. <?php echo $firstname; ?>, <?php echo $lastname; ?>

出现在

<tr>
<td><div  id="show"></div></td>
</tr>

根据客户ID /名称选择

that based on customer id/name selected

这样做,我尝试使用json调用如下:

to do that i try to use json call as following:

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

the php call json placed at customer.php with 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;
}

但我收到错误的回报如下

but i get the wrong return as following

有没有人请如何解决它更好?感谢提前

is there someone please how to solved it to more better? thanks in advance

推荐答案

而不是这样:

    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调用中执行以下操作:

in Your JS AJAX call do this:

    success: function(data) {
        $('#show').append('<input type="text" name="customer_id" value="' + data['customer_id'] + '" /><input type="text" name="firstname" value="' + data['firstname'] + '" />');
    }

因为您的PHP函数只返回一个对象。如果您然后循环对象属性您确实循环了属性值的一个字符...

as Your PHP function returns ONLY ONE object. If You then loop over the objects property You loop over one character of the property value indeed...

这篇关于使用下拉列表自动填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆