1

i have an Autocompleter, working fine, now i wanted to add an "Autofiller" That means, if i select one of the "Companies" from the "Autocompleter" it should get all results from the DB for that Companies.

The JSON response is:

[
    {
        "idfirma": "2222",
        "firmenname": "test",
        "strasse": "test",
        "plz": "",
        "ort": "test",
        "l‌​and": "",
        "webseite": "",
        "region": "",
        "aktiv": "1"
    }
]

What i got so far:

$(document).ready(function() {

    $(function() {
        $( "#firma_neu" ).autocomplete({
            source: "./firma_suche.php3",
            open: function(event, ui) {
                    var firmenname = $("#firma_neu").val();
                    },
            select: function(event, ui) {
                    event.preventDefault();
                    $("#firma_neu").val(ui.item.label);
                    $("#idfirma_neu").val(ui.item.value);

                    // Hier muss der if selected aufruf kommen

                        var dataString = 'firmenID='+ ui.item.value ;
                          $.ajax({
                            type: "POST",
                            data:  dataString,
                            url: "./loadFirma.php",
                            dataType: "json",

                             success: function(result){

                              $("#Strasse1").val(result.strasse);
                              $("#PLZ1").val(result.item.plz);
                              $("#Ort1").val(result.item.ort);
                              $("#Land1").val(result.item.land);
                              $("#Region1").val(result.item.region);

                             }
                           });

                    },
            minLength: 2
        });
    });

});

I get the correct answer from loadFirma.php but i cant use them...
result.strasse
and result.item.strasse
both wont work

6
  • you are not sending any data. Add data: dataString in your AJAX request. Commented Apr 14, 2014 at 12:11
  • ah sry added the data: dataString; Commented Apr 14, 2014 at 12:13
  • if i use: alert(result['strasse']) or alert(result.strasse) both are empty. ---- alert(result) gives me out [object Object] Commented Apr 14, 2014 at 12:27
  • [{"idfirma":"2222","firmenname":"test","strasse":"test","plz":"","ort":"test","land":"","webseite":"","region":"","aktiv":"1"}] Commented Apr 14, 2014 at 12:35
  • you should avoid language mashup in code weil das schafft nur verwirrung Commented Apr 14, 2014 at 12:37

1 Answer 1

3

The server side is outputting JSON but it is an array. See how it is surrounded by []? So result.strasse does not exist.

Change to access the first element in the array before you try to access strsse:

$("#Strasse1").val(result[0].strasse);

If you expect multiple results then you can iterate over the array:

for(var i=0; i<result.length; i++){
    console.log(result[i].strasse)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.