Solved

Pre select list box in Ajax results

Posted on 2013-11-23
9
213 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
Comment Utility
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
Comment Utility
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
Comment Utility
You write that code into Ajax Success method.

http://api.jquery.com/jQuery.ajax/
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:jws2bay
Comment Utility
ChrisStanyon :  No I'm not using jQuery for this ajax.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
OK. Can't help you then. Sorry
0
 

Author Comment

by:jws2bay
Comment Utility
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
Comment Utility
@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
Comment Utility
I ended up going with the quick fix... still need to get a grip on jQuary
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now