auto populate data with dropdown

淺唱寂寞╮ 提交于 2019-12-12 02:09:33

问题


ALL EDITED

Hi,

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

has html view code as

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

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

appear in

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

that based on customer id/name selected

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>

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

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

but i get the wrong return as following

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


回答1:


Something on your PHP-Coding style: For a PHP Code-Block, don't use new php-Tags for every line. Also, if you want an HTML-Output from PHP, you can use the echo-Method to do this. So your Code looks like this:

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

The thin is, every time the PHP-Interpreter finds an opening PHP-Tag, it starts Interpreting it's code, and when it finds a closing Tag, it stops doing this. So in your code, the interpreter starts and stops all the time. This is not very performance.

I guess you want to set the value of your text-fields? That's really not what PHP does. That is more a JavaScript thing, because it actually happens in the Browser, not on the Server.




回答2:


Instead of this:

    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'] + '" />');
        }
    }

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'] + '" />');
    }

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



来源:https://stackoverflow.com/questions/5873723/auto-populate-data-with-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!