0

すべて編集済み

やあ、

選択したドロップダウンでデータベースからデータを自動入力するにはどうすればよいですか?ドロップダウンの結果もすでに表示されています。コードは次のとおりです。

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

ドロップダウンの1つを選択した場合、別の例が必要です。<?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は、URL index.php?p = page / customerを使用してcustomer.phpに配置されます)

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

しかし、私は次のように間違ったリターンを受け取ります

ここに画像の説明を入力してください

誰かがそれをより良く解決する方法を教えてくれますか?前もって感謝します

4

2 に答える 2

0

PHPコーディングスタイルに関する何か:PHPコードブロックの場合、すべての行に新しいphpタグを使用しないでください。また、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タグを見つけるたびに、そのコードの解釈を開始し、終了タグを見つけると、これを停止します。したがって、コードでは、インタープリターは常に開始および停止します。これはあまりパフォーマンスではありません。

テキストフィールドの値を設定したいと思いますか?それは実際には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関数は1つのオブジェクトのみを返します。その後、オブジェクト プロパティをループすると、実際にはプロパティ値の 1 文字をループします...

于 2011-05-05T09:09:20.850 に答える