简体   繁体   中英

Can jQuery UI Autocomplete use values from multiple input fields

I've been struggling with this code for a while and honestly I'm stuck.

This is how my auto-complete script looks like:

jQ19(function(){
    // minLength:1 - how many characters user enters in order to start search
    jQ19('#location_name').autocomplete({

        source:'adress.php',
        minLength:2,
        select: function(event, ui){

            // just in case you want to see the ID
            var accountVal = ui.item.value;
            console.log(accountVal);

            // now set the label in the textbox
            var accountText = ui.item.label;
            jQ19('#location_name').val(accountText);

            return false;
        },
        focus: function( event, ui ) {
            // this is to prevent showing an ID in the textbox instead of name
            // when the user tries to select using the up/down arrow of his keyboard
            jQ19( '#location_name' ).val( ui.item.label );
            return false;
        }

    });

});

Some more js to get me the additional value:

<script type='text/javascript'>
    $('#city').on( 'change', function () {
        var x = document.getElementById('city').value;
        $.ajax({
            type: 'post',
            url: 'http://localhost/profsonstage/account/_compettions/autocomplate/location_name.php',
            data: {
                value: x
            },
            success: function( data ) {
                console.log( data );
            }
        });
    });
</script>*/

And the php that fetches the data results:

try {
    $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}catch(PDOException $exception){ //to handle connection error
    echo "Connection error: " . $exception->getMessage();
}

// get the search term
$city = $_GET['city'];
$b = explode(" ",$city); //get rid of the city code
$a =$b[0];

$search_term = isset($_REQUEST['term']) ? $_REQUEST['term'] : "";

//query to search for data
$query = "SELECT * from locations where adress like '%$search_term%' and city=='$a'
LIMIT 0 , 5";

The problem is when the user types a value in the input for the #City the query looks like this:

$query = "SELECT * from locations where adress like '%%' and city==' SomeCity ' LIMIT 0 , 5";

and when we go to the next input that (is supposed to be autocomplete too and start typing) the query looks like

$query = "SELECT * from locations where adress like '% input to autocomplate %' and city=='' LIMIT 0 , 5";

Basically I can't figure out a way to pass the values at the same time. Any help will be appreciated.

You can use following;

jQ19(function(){
    // minLength:1 - how many characters user enters in order to start search
    jQ19('#location_name').autocomplete({

        minLength:2,
        select: function(event, ui){

            // just in case you want to see the ID
            var accountVal = ui.item.value;
            console.log(accountVal);

            // now set the label in the textbox
            var accountText = ui.item.label;
            jQ19('#location_name').val(accountText);

            return false;
        },
        source: function( request, response ) {
            var term = request.term;

            $.getJSON( "address.php?term=" + term + "&city=" + $("#city").val(), request, function( data, status, xhr ) {
              response( data );
            });
        },
        focus: function( event, ui ) {
            // this is to prevent showing an ID in the textbox instead of name
            // when the user tries to select using the up/down arrow of his keyboard
            jQ19( '#location_name' ).val( ui.item.label );
            return false;
        }

    });

});

And in your php file;

try {
    $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}catch(PDOException $exception){ //to handle connection error
    echo "Connection error: " . $exception->getMessage();
}


// get the search term
$city = $_GET['city'];
$b = explode(" ",$city); //get rid of the city code
$a =$b[0];

$search_term = isset($_REQUEST['term']) ? $_REQUEST['term'] : "";

//query to search for data
$query = "SELECT * from locations where adress like '%$search_term%' and city=='$a'
LIMIT 0 , 5";

//Fetch your data and respond json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM