Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7408449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:55:09+00:00 2026-05-29T05:55:09+00:00

I have this jquery.ajax form which populates data from the database and auto fills

  • 0

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 field is of type text
But it does not auto fill when the form field is a select box.
here is my code so far..

<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:55:10+00:00Added an answer on May 29, 2026 at 5:55 am

    When you say

    But it does not auto fill when the form field is a select box

    Do you mean to fill the <select> with a list of data from server-side or to select an <option> that is already on the <select>?

    If your case is that you want to fill the <select> then you have to create each node that represents each <option> filled with its corresponding array item that you brought from server-side. Ex.

    $('#state>option').remove();
    $.ajax(
      ...
      success: function(data){
        for(var i=0; i<data.array.length; i++){
          $('#state').append($('<option>')
            .attr('value', data.array[i].value)
            .html(data.array[i].name));
        }
      }
    );
    

    If what you want is to select an <option> that is already on your <select> then you need to iterate over your current options and either apply it the ‘selected’ attribute or set the <select>‘s selectedIndex as the index of the option you want to select. For example:

    $.ajax(
      ...
      success: function(data){
        $('#state>option>option').each(function(i, elm){
          if($(elm).attr('value').indexOf(data.state) >= 0){
            $(elm(.attr('selected', 'selected');
          }
        });
      }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button which calls jquery.ajax and submits POST data. I need to
On my main page I have this jquery code which does an ajax call
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
I have this ajax jquery code: var form = document.getElementById('frm'); var data_string = form.serialize();
I'm facing a trouble with jquery ajax under IE8. I have a form which
I have the following jquery Autocomplete form that pulls data in from a local
I have a pretty big data entry form which has many text fields ,
I am using the jQuery $.ajax() function. I have put this into a parent
A situation I ran across this week: we have a jQuery Ajax call that
I have this jQuery which works fine $(li[id^='shop_id']).click( function () { alert(I clicked on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.