简体   繁体   English

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

[英]Auto populate based on dropdown selected, need help

How can I auto populate the data from db by dropdown selected?如何通过选择的下拉列表自动填充 db 中的数据? and my dropdown result already appear as well, the code as following:我的下拉结果也已经出现,代码如下:

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

and the result of dropdown above listed as上面列出的下拉列表的结果为

  • admin行政
  • customer1客户1
  • FREE自由

loaded from following db从以下数据库加载

INSERT INTO `my_customer` (`customer_id`, `name`, `firstname`, `lastname`) VALUES
(8, 'admin', '', ''),
(6, 'customer1', 'ok', ''),
(7, 'FREE', 'marj', 'om');

so whenever dropdown selected i want the all data below: so whenever dropdown selected i want the all data below:

<tr>
<td><?php echo $firstname; ?></td>
<td><?php echo $lastname; ?></td>
</tr>

also auto populate, it seem need javascript/ajax/jquery to fixed it, I was Wondering if someone could help me, and thanks in advance也自动填充,似乎需要 javascript/ajax/jquery 来修复它,我想知道是否有人可以帮助我,提前感谢


Addtion JSON CALL添加 JSON呼叫

I have the json call already as following: (let say this placed at customer.php with url index.php?p=page/customer ) I have the json call already as following: (let say this 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));
}

and the 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;
}

Suppose You are using jQuery, You will listen to select change event and then do an ajax call for PHP function that will return the data. Suppose You are using jQuery, You will listen to select change event and then do an ajax call for PHP function that will return the data. The data will then be outputed to the appropriate places.然后将数据输出到适当的地方。 I advise to set id attributes for next tags: <select> , <td> for name, <td> for surname, like so:我建议为下一个标签设置 id 属性: <select><td>用于名称, <td>用于姓氏,如下所示:

<select name="customer_id" id="customer_id>...</select>

<td id="firstname"> echo firstname </td>
<td id="lastname"> echo lastname </td>

Then the jquery code:然后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>

Supposing that Your my_php_script.php retrieves the data from database by given customer_id in $_POST['customer_id'] and returns a JSON object like echo json_encode(array('firstname' => FIRSTNAME_FROM_QUERY, 'lastname' => LASTNAME_FROM_QUERY)); Supposing that Your my_php_script.php retrieves the data from database by given customer_id in $_POST['customer_id'] and returns a JSON object like echo json_encode(array('firstname' => FIRSTNAME_FROM_QUERY, 'lastname' => LASTNAME_FROM_QUERY));

ADDITION : There are two options how to solve it - in JS instead of补充:有两种方法可以解决它 - 在 JS 中而不是

$.post()

You have to use你必须使用

$.get(...)

OR in Your PHP script instead of在您的 PHP 脚本中,而不是

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

You have to use你必须使用

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

at every place... This should do it... Eg:在每个地方......这应该这样做......例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM