How to pass additional parameters in jQuery UI Autocomplete


At the time of using jQuery UI Autocomplete in PHP, many times you need to pass additional parameters to an autoComplete call. Here is a simple way to pass extra parameters to the source URL. In that case, you need to define a callback function to the source option of autoComplete call. Also, you can pass multiple parameters using this approach. Your jQuery UI Autocomplete function would be like the following.

<script>
$(function() {
    $( "#skills" ).autocomplete({
        source: function(request, response) {
            $.getJSON(
                "search.php",
                { term:request.term, extraParams:$('#extra_param_field').val() }, 
                response
            );
        },
        minLength: 2,
        select: function(event, ui){
            //On select action
        }
    });
});
</script>

In the source file, you can get the term and additional parameters using the $_GET variable.

$term $_GET['term'];
$extraVar $_GET['extraParams'];

1 Comment

  1. Adnan Said...

Leave a reply

Connect With CodexWorld