0

全部编辑

你好,

如何通过选择的下拉列表自动填充 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));
}

和分贝

public function getCustomer($customer_id) {
    $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
    return $query->row;
}

但我得到错误的回报如下

在此处输入图像描述

有人请如何更好地解决它?提前致谢

4

2 回答 2

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 时,它就会开始解释它的代码,当它找到一个关闭的 Tag 时,它就会停止这样做。因此,在您的代码中,解释器一直在启动和停止。这不是很表现。

我猜你想设置你的文本字段的值?这真的不是 PHP 所做的。这更像是 JavaScript 的事情,因为它实际上发生在浏览器中,而不是服务器上。

于 2011-05-03T19:27:21.400 回答
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 函数只返回一个对象。If You then loop over the objects property You loop over the one character of the property value确实...

于 2011-05-05T09:09:20.850 回答