user2714562
user2714562

Reputation: 21

Jquery autocomplete with php as remote source: how to append second variable from input to source

I am building a form which requires autocomplete with a php-file as source.

The filtering is done via the strassenliste.php, whereby every single keydown does a new php-call with ?term=123 being attached to the URL (this is still quick enough for me).

There is one last issue to get the thing working:

I have two input fields (first is #plz, second is #strasse), where plz means german "zip" and strasse "street". #strasse is the field to be autocompleted (via ?term=...).

When the user leaves the first input field, namely #plz (onblur), this input (zipcode) shall be stored in an JavaScript variable and appended to the URL strassenliste.php in the jQuery autocomplete code like e.g. --> source: 'strassenliste.php?plz=12345&'.

The input of the #strasse then will be appended too, which already works: ?term=[input].

What I still don´t get working is that the parameter zip (followed by a &) is being appended to the URL strassenliste.php.

As a result the source in the autocomplete-code should be the URL --> strassenliste.php?plz=12345&[term=...].

Here is my code that I already have:


JavaScript-part:

<script>
$("#plz").blur(function () {
var plz = $(this).val();
}).keyup();
var url = "strassenliste.php" + "?=&" + plz;

$(function() {
$( "#strasse" ).autocomplete({
source: url   
});

...

</script>

PHP-part:

$plz = $_GET['plz'];
$term = $_GET['term'];


[SOAP-Call and response]
...

foreach ($sxe->searchresults as $searchresults) {
$array[] = utf8_encode($searchresults->strasse);

sort ($array);

$strassen = "[";
foreach ($array as $key => $val) {
$strassen .= utf8_decode("\"" . ($val) . "\",");
}
$strassen .= "\"\"";

$length = strlen($strassen);
$strassen = substr($strassen, 0, $length - 3);

$strassen .= "]";

echo ($strassen);

The PHP is not the problem, this works. I tested it by giving a fix value to the variable $plz and simply setting the source to strassenliste.php in the jQuery-part.

But I still have a problem with the jQuery-Part, which is the last obstacle to get it working.

The second is that the questionmark in the autocomplete [term =] may not appear, since will be already appended in front of "plz=12345".

Does anyone have a clue?

Thank you so much for any hint!

Upvotes: 2

Views: 494

Answers (1)

Nick
Nick

Reputation: 6025

The problem is that your line var url = "strassenliste.php" + "?=&" + plz; can't see the variable plz because it's within the scope of the blur function.

You are also formatting the GET variables weirdly in the URL. The normal way is myURl.php?var1=Helmut&var2=Thielicke.

Try this:

    $(function() {        
        $("#plz").blur(function () {
            var plz = $(this).val(),
                url = "strassenliste.php" + "?plz=" + plz;

            $("#strasse").autocomplete({
                source: url   
            }); 
        }).keyup();
    )};        

Upvotes: 1

Related Questions