Solved

Pre select list box in Ajax results

Posted on 2013-11-23
9
218 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
[X]
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
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

691 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