Pre select list box in Ajax results

I am working with php/mysql.  I am doing an Ajax fetch from mysql.  In the reply I am building a table which contains a list box.  I want to preselect the value in the box based on a value being fetched.  

I wrote the fetched value to a hidden field in the table, and tried using a js preselect function to loop through the options , selecting the one that matches the value in the hidden field.  
I can't get the js function to see hidden field value.

The js preselect function is fired right after the Ajax call.?   Do I need to let things settle before I can access the table contents??

If there is a better way to do this please let me know.

Thanks

My php file attached:

JS function below:

function updatestatus(){
      
      var itemToSelect =  form1.current_status.value;
    var myDropdownList = document.New_Status.lstNames;

    for (iLoop = 0; iLoop< myDropdownList.options.length; iLoop++)
    {    
      if (myDropdownList.options[iLoop].value == itemToSelect)
      {
        myDropdownList.options[iLoop].selected = true;
        break;
      }
    }

  }
jws2bayAsked:
Who is Participating?
 
youssefomarConnect With a Mentor Commented:
@jws2bay, since you are not using jquery, I'm guessing that you are using XMLHttpRequest, if yes, then you can use onreadystatechange function to populate the value.

take a look at this example: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
0
 
Mohit VijayCommented:
You need to wait for call to complete. Or while page load, that time only you should have that preselect values in hidden field.
0
 
jws2bayAuthor Commented:
Right now the action that fires off the Ajax fetch also fires the function which needs to read the Ajax results.  What is the best way to make sure the Ajax fetch is complete before I call the function that uses the results?
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Mohit VijayCommented:
You write that code into Ajax Success method.

http://api.jquery.com/jQuery.ajax/
0
 
Chris StanyonConnect With a Mentor Commented:
This is generally a lot easier if you're using the jQuery library - are you??

$.ajax({
    url: 'somePage.php'
}).done(function(data) {
   //the response of the AJAX call is stored in data. Inject it into the Document
   $('#response').html(data);

   //select an option based on a given value  
   someValue = $('#hiddenInput').val();
   $('#mySelect option[value="' + someValue + '"]').attr('selected', true)
});

Open in new window

0
 
jws2bayAuthor Commented:
ChrisStanyon :  No I'm not using jQuery for this ajax.
0
 
Chris StanyonCommented:
OK. Can't help you then. Sorry
0
 
jws2bayAuthor Commented:
I don't know jquery, but I'm going to use this example as a learning tool.  Got to come to the party sometime.

I'll look at it closer on Monday.

Thanks
0
 
jws2bayAuthor Commented:
I ended up going with the quick fix... still need to get a grip on jQuary
0
All Courses

From novice to tech pro — start learning today.