Solved

Pre select list box in Ajax results

Posted on 2013-11-23
9
215 Views
Last Modified: 2013-12-01
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;
      }
    }

  }
0
Comment
Question by:jws2bay
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 39671764
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
 

Author Comment

by:jws2bay
ID: 39671801
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
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 39671810
You write that code into Ajax Success method.

http://api.jquery.com/jQuery.ajax/
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
ID: 39671832
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
 

Author Comment

by:jws2bay
ID: 39671873
ChrisStanyon :  No I'm not using jQuery for this ajax.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39671985
OK. Can't help you then. Sorry
0
 

Author Comment

by:jws2bay
ID: 39672003
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
 
LVL 6

Accepted Solution

by:
youssefomar earned 400 total points
ID: 39672316
@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
 

Author Closing Comment

by:jws2bay
ID: 39688569
I ended up going with the quick fix... still need to get a grip on jQuary
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is assert.deepEqual? 4 47
Browser annoying behavior 18 58
How can I save all open docs into a given folder 12 116
Certificate Verification Error for Yahoo Mail 3 36
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question