Auto populate based on dropdown selected, help

Discussion in 'JavaScript' started by omccom, May 4, 2011.

  1. #1
    Hi,

    How can I auto populate the text fields 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">';
    
        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:
    and the result of dropdown above listed as

    • admin
    • customer1
    • FREE
    loaded from following db

    
    INSERT INTO `my_customer` (`customer_id`, `name`, `firstname`, `lastname`) VALUES
    (8, 'admin', '', ''),
    (6, 'customer1', 'ok', ''),
    (7, 'FREE', 'marj', 'om');
    PHP:
    so whenever dropdown selected i want the all data below:
    <tr>
    <td><?php echo $firstname; ?></td>
    <td><?php echo $lastname; ?></td>
    </tr>
    PHP:
    also auto populated, it seem need javascript/ajax/jquery to fixed it, I was Wondering if someone could help me, and thanks in advance
     
    omccom, May 4, 2011 IP
  2. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #2
    If I understand correctly you have:
    
    <select>
      <option>admin</option>
      <option>customer1</option>
      <option>FREE</option>
    </select>
    
    Code (markup):
    You can transform this code like:
    
    <select onchange="ajaxUpdate()">
      <option>admin</option>
      <option>customer1</option>
      <option>FREE</option>
    </select>
    
    Code (markup):
    where ajaxUpdate() is javascript function which will call PHP script for given member. PHP will return his/her lastname and firstname into javascript e.g. as JSON or Xml. Javascript will parse it and display these values instead of values of previous member.
     
    Jan Novak, May 4, 2011 IP
  3. omccom

    omccom Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Jan Novak,

    thanks for your nice suggestion,

    yeah the return code of

    <?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>';
    ?>
    PHP:
    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>
    HTML:
    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>
    PHP:
    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>
    Code (markup):
    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));
    }
    PHP:
    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;
    }
    PHP:
    but i get the wrong return as following
    wrong.png

    is there someone please how to solved it to more better? thanks in advance
     
    Last edited: May 4, 2011
    omccom, May 4, 2011 IP