LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2021, 10:55 AM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Rep: Reputation: 9
can't get returns from ajax success callback function


Object: Create dropdown list for general use by only sending one parameter.

I am trying to call 1. Ajax with a parameter then 2. process that Ajax data to select an option from a database and 3. return that option selected to the calling function - in order to populate an input tag. I am running into trouble in returning my selection to the calling function. I know this is tied into the asynchronous nature of Ajax but I don't know how to fix this.

Here is the code I'm using:

Code:
    <!-- ======  HTML  =========== -->

         <label for='dr'>Doctor</label>
        <input class="hov" id="dr" type="text" name="dr" /><br>  <!-- Populate This -->

    <div id="category">        <!-- Show Select Box Here -->

    </div>
    <!-- ======== SCRIPT ============ -->

    <script type="text/javascript" >

    var myselection="";   // Global var's
    var str="";
    var ret="";

    $(document).ready(function(){

    $("input[name=dr").focus(function(){  // FOCUS is on INPUT
            str="doctors";              // str - any array name in PHP file to get the database info
            ret=callAjaxDr(str);     // return value from select box to populate input
            $("input[name=dr]").val(ret);
    });

    </script>
    //  =========  JS FILE  =============

    in .js file

    function callAjaxDr(str) {  // gets data from mysql database using PHP script
            $.ajax({
            url: "listArrays.php",
            type: "POST",
            data: {"lstnm":str},
            dataType: "json",
            success: getDr         
        }); // ajax
    }

    function getDr(data) {
                $('#category').show();
                 $('#category').append($("<h4>Selection Offered</h4>"))          // create select table
                                .append($("<select id='cats' name='cats' size='5'></select>"));
                 for (const val of data) {
                   $('#cats').append($("<option>").prop({ value: val, text: val }));
                }
            $('#category').append("<br><br>");
        
            $("#cats").on('change', function () {                                // make selection
                myselection=$("#cats option:selected").text();
                $('#category').empty();
            }); // change
           
            return myselection;  // TROUBLE AREA - return selected option to calling function
    }
 
Old 08-15-2021, 12:04 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,116

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
Define getDr inside callAjaxDr, and have it modify a variable that's in callAjaxDr's scope.
 
Old 08-15-2021, 04:00 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 384

Original Poster
Rep: Reputation: 9
Thanks for your reply Dugan,

Quote:
Define getDr inside callAjaxDr, and have it modify a variable that's in callAjaxDr's scope.
I have included all files to run this if you want to see what happens. In lieu of that:

Start program:
click on Doctor input
-> select box appears
-> alert appears "in drop9999 line 40 return is and type is string" - in focus
click ok
-> clears alert, select box stays
click on option
-> select box clears
click on Doctor
-> Doctor input populated
-> alert appears "in drop9999 line 40 return is Allen, Peter W OD and type is string" - in focus
click ok
-> alert clears
-> Doctor populated input still shows
-> select box still shows
click on option
-> select box clears
-> Doctor populated input still shows
click done
-> Start Over

Sooooooo .. This partially works in that I can get a return value back but with the penalty of bizarre behavior. Do you have suggestion as to how to fix this? I Hope!!!

Because of Linux Questions File restrictions

dropdown9999.txt is dropdown9999.php
listArrayTest.txt is listArrayTest.php
testnew.txt is testnew.js

R
Attached Files
File Type: txt dropdown9999.txt (1.5 KB, 9 views)
File Type: txt listArrayTest.txt (695 Bytes, 8 views)
File Type: txt testnew.txt (1.6 KB, 8 views)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Callback function pointer to Class function pronoob Programming 16 09-30-2016 02:09 AM
[SOLVED] beep returns -1, strerror returns SUCCESS RandomTroll Linux - Software 9 05-03-2016 11:22 PM
problem with callback function in pcap_loop function kikilinux Programming 1 03-28-2016 07:14 AM
Why does the callback function go BEFORE the send() in AJAX? resetreset Programming 1 01-27-2011 06:02 AM
LXer: Mastering Ajax, Part 3: Advanced requests and responses in Ajax LXer Syndicated Linux News 0 02-16-2006 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration