Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pre select list box in Ajax results

Posted on 2013-11-23
9
216 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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