www.webdeveloper.com
Results 1 to 4 of 4

Thread: Feeding variable to AJAX call

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    3

    Feeding variable to AJAX call

    Hi everyone,

    I'd appreciate your help on the following.

    I have a JQuery script which makes an AJAX call. When I feed the datastring as 'web' (an actual string value), it works (the php script in the AJAX call returns all jobs starting with 'web%' from the database).

    But it's not working when feeding the ID's value of the inputfield (preferrably while typing in the inputfield). What am I doing wrong?

    Thank you in advance for any help,

    Onkelarie


    Code:
    <script>
    
        $(document).ready(function () {
            'use strict';
    
            $.ajax({
                url: 'assets/ajax/icares_lookup.php',
                dataType: 'json',
                cache: false,
                data: { datastring: $('#autocomplete_icares').val() }
            }).done(function (data) {
                var status = $('#selection'),
                professions = $.map(data, function (value) {
                    return value;
                });
    
                $('#autocomplete_icares').autocomplete({
                    lookup: professions,
                    onSelect: function (suggestion) {
                        //status.html('You selected: ' + suggestion);
                    }
                });
            });
    
        });
    </script>
    
            <div class="form-input">
                <input type="text" name="icares_career_goal" id="autocomplete_icares">
                <div id="selection"></div>
            </div>

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You're making the Ajax call right when the page has loaded, so the autocomplete_icares input is probably empty.

  3. #3
    Join Date
    Dec 2012
    Posts
    3
    First off, thanks for checking any odd coding, much appreciated.

    The whole trick should be that, when the user is typing in the inputfield with the ID #autocomplete_icares, it should make the AJAX call.... I'm somehow missing something. Should there be a keyup action somewhere in the JQuery?

  4. #4
    Join Date
    Dec 2012
    Posts
    3
    This thread can be locked...

    I managed to solve the problem (well, actually, my colleague)

    The only thing missing was the actual keyup action, and a simple function around the AJAX call does the trick!

    Code:
    <script>
    
        $(document).ready(function () {
            'use strict';
    
            $('#autocomplete_icares').keyup(function() {
                $.ajax({
                    url: 'assets/ajax/icares_lookup.php',
                    dataType: 'json',
                    cache: false,
                    data: { datastring: $(this).val() }
                }).done(function (data) {
                    var status = $('#selection'),
                    professions = $.map(data, function (value) {
                        return value;
                    });
    
                    $('#autocomplete_icares').autocomplete({
                        lookup: professions,
                        onSelect: function (suggestion) {
                            //status.html('You selected: ' + suggestion);
                        }
                    });
                });
            });
    
        });
    </script>
    
            <div class="form-input">
                <input type="text" name="icares_career_goal" id="autocomplete_icares">
                <div id="selection"></div>
            </div>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles