Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change value of textbox when dropdown value is changed

    I have a dropdown 'client_name' which has options from database. I also have textarea field (address of client which is stored) in database. When user selects client_name, the address field should get address from database and display it. I've tried this

    Dropdown

    Code:
    <select name="client_id" id="client_id" >
                	<?php
    			$q_get_clients = "SELECT * FROM client WHERE status = '1' ORDER BY client_name ASC";
    			$result = $mysqli->query($q_get_clients);
    			while ($row = $result->fetch_assoc()){
    			echo '<option value="'.$row['client_id'].'" >'.$row['client_name'].'</option>';
    			echo '<option value="'.$row['address'].'" style="display:none" id="address"></option>';
    			}
    		?>
                	</select>
                    <script>
    			$("#client_id").change(function (){
    			var add = $("#address").val();
    			$("#address1").val(add);
    			});
    Address

    Code:
    <textarea name="address" id="address1"></textarea>
    client table
    client_id client_name address

    But i'm not getting address in textarea.

    What is wrong in above code? Is there any other way to do this?

    Thank You.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    What is wrong in above code?
    Your while loop will create multiple <option> elements with the same id attribute value (address). This will make your document invalid.

    Try
    PHP Code:
    while ($row $result->fetch_assoc()){
            echo 
    '<option value="'.$row['client_id'].'"  data-value="'.$row['address'].'">'.$row['client_name'].'</option>';
            } 
    Code:
    <textarea id="address"></textarea>
    Code:
    $(document).ready(function(){
    	$('#client_id').change(function(){
    		var ad= $(this).find(':selected').data('value');
    		$('#address').val(ad);
    	});
    });
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. It's working.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •