【问题标题】:auto populate data with dropdown使用下拉菜单自动填充数据
【发布时间】:2011-08-17 22:33:15
【问题描述】:

全部编辑

嗨,

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

但我得到错误的回报如下

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

【问题讨论】:

    标签: php text drop-down-menu field populate


    【解决方案1】:

    关于你的 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 的事情,因为它实际上发生在浏览器中,而不是服务器上。

    【讨论】:

    • 感谢您的建议,非常感谢,是的,我想通过选择组合框自动填充文本字段(名字,姓氏),从 db 加载数据,我知道如何加载数据,并且你是对的,它似乎需要 javascript/ajax/jquery 来处理它,但是你能告诉我如何修复它吗?
    • 您的意思是如何设置文本字段中的值,具体取决于您在下拉框中选择了哪个选项?
    • 是的,没错,我完全编辑了我以前的代码,请看一下,我希望足以描述我想要的内容,请让我知道哪里出了问题或者有更好的解决方案吗?谢谢
    【解决方案2】:

    而不是这个:

        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 does not...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多