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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts

    autofill form fields for select boxes

    I have this jquery.ajax form which populates data from the database and auto fills the form fields upon button click.
    It works great when the form is of type <input type="text">
    But it does not auto fill when the form field is a select box.
    here is my code so far!

    Code:
    <script type="text/javascript">
    $(document).ready(function() {
        function myrequest(e) {
            var lead_id = $('#lead_id').val();
            $.ajax({
                 method: "GET",
                url: "/pdp/fetch-client-data/",
    			dataType: 'json',
    			cache: false,
                data: {
                    lead_id: lead_id
                },
                success: function( responseObject ) {
                    alert('success');
                    $('#client_name').val( responseObject.client_name );
                    $('#state').val(responseObject.state);
    
                },
    			failure: function() 
    			{
                    alert('fail');
                }
            });
        }
        
        $('#fetchFields').click(function(e) {
            e.preventDefault();
            myrequest();
        });
    	$("#lead_id").bind("change", function(e)
    	{
    	  myrequest(); 
    	});
    });
    
    </script>
    
    <table width="600" align ='center'>
    <form action ='<?php echo $SERVER['PHP_SELF']; ?>' method='post'>
    
        <tr>
    		<td>		
    			<label for="lead_id">Lead id: </label>
    			
    			<input type="text" name="lead_id" id="lead_id">
    		
    			<button id="fetchFields">Fetch</button>
    		</td>
    	</tr>
    	<tr>
    		<td>
    			Agent: <select name="agent">
    				<option value="">[Select]</option>
    				<?php foreach($this->agent_query as $agent){ ?>
    			    <option value="<?php echo $agent['id']; ?>"><?php echo $agent['name'];?></option>
    			    <?php } ?>
    			</select>
    		</td>
    	</tr>
    	<tr>
    		<td>
    			<label for="client_name">Client Name:  </label>
    			<input type="text" name="client_name" id="client_name">
    		</td>
    	</tr>
        <tr>	
    		<td>
    			<label for="state">State: </label>
    			<select name="state" id='state'>
    			<option id='state' value='1'></option>
    			</select>
    		</td>
    	</tr>
    	    <tr>
    		<td>
    			# of Policies: 
    			<select name="policies">
    				<option value="1">1</option>
    				<option value="2">2</option>
    				<option value="3">3</option>
    				<option value="4">4</option>
    			</select>
    		</td>
    		</tr>		
    	<tr>
    		<td>
    			<input type="submit" value="NEXT" name="submit">
    		</td>
    	</tr>
     
    </form>
    </table>

  2. #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You should not have a form inside a table. Please wrap the <table> with the <form> tag

  3. #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Ok I updated the table part.
    Now I already have options values for states like TX, NY etc.
    If my query returns the state Tx then the selected option shoould be tx.

  4. #4
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    the above code does not work for states. The client field gets filled automatically but the select box field does not get selected with the value returned from database

  5. #5
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    never mind..it works now..

  6. #6
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    the last time you mentioned displaying an error if the query returns empty results..I am not able to display it in my form area..how do i do that


 

Posting Permissions

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