1

Ok, I'm struggeling with JSON, AJAX, Javascript and PHP. Well, actually its only Javascript.

I want to have a input field which operates as a AutoSearch field. I am using currently EasyAutoComplete.

But that doesnt really matter. What does matter is, that I have to choose one type from the JSON array to be searched. But I want to search both.

Just to make things clear: - the user should be able to type in "abc" and "123" and find either the number "123456" or "School abc". - When the user clicks on the result, in both cases the field value should be the number.

Javascript:

       $(document).ready(function(){
        var options = {
            url: function(phrase){
                console.log("autocomplete/autocomplete.php?term="+phrase);
                return "autocomplete/autocomplete.php?term="+phrase;
            },
            highlightPhrase: false,
            getValue: "instanz_id",
            template: {
                type: "description",
                fields: {
                description: "schulname"
                }
            },
            list: {
                hideAnimation: {
                    type: "slide",
                    time: 400,
                    callback: function(){}
                },

                match:{
                    enabled: true
                }
            }
        };
        $("#autocomplete").easyAutocomplete(options);
    });

My database structure looks like:

"instanz_id", "schulname"

"123456", "DemoSchule"

Right now, if I type in "123" my JSON return string looks like this:

 [{"instanz_id":"123456","schulname":"DemoSchule"}]

And my searchlist: "123456 - DemoSchule". When I click on it, 123456 is the new value of the input field.

How can I get the same item in the searchlist when typing e.g. "Dem" ? I can change the getValue to "schulname" but then I cannot search using the number...

Can please someone send me in the right direction?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Dawko
  • 77
  • 1
  • 1
  • 7
  • What does your autocomplete.php look like? – JeanPaul98 Nov 22 '17 at 14:51
  • Doesnt really matter because the JSON string is correct: connect_error){ die("Connection failed"); } $sql = "SELECT instanz_id, schulname, suchstring FROM schule WHERE instanz_id LIKE '%".$_GET['term']."%' OR schulname LIKE '%".$_GET['term']."%';"; $result = $db->query($sql); $arr = []; if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ $arr[] = $row; } } echo json_encode($arr); – Dawko Nov 22 '17 at 14:54
  • what's the autocomplete plugin you're using? Is there a URL for its documentation page? – ADyson Nov 22 '17 at 16:59
  • http://easyautocomplete.com/ – Dawko Nov 22 '17 at 20:03
  • the plugin doesn't seem to have much flexibility in the way you want. You could maybe try using something else. jQueryUI autocomplete (just for example) is more flexible and would be able to meet your requirement. There are others. – ADyson Nov 22 '17 at 21:34
  • ok, I'll take a look and expose my results then here – Dawko Nov 23 '17 at 09:11
  • Ok, I now use the jquery-ui autocomplete js and with a script simple as $( "#autocomplete" ).autocomplete({ autoFocus: true, source: "autocomplete/autocomplete.php" }); the "Schulnummer" field shows results. The amount of results seems to be correct - but the values are empty... It looks strange. – Dawko Nov 23 '17 at 09:55
  • It seems the JSON string has to contain "value" and "label"? Can somebody help me with the build of such an array in my autocomplete.php? – Dawko Nov 23 '17 at 12:52
  • – Dawko Nov 23 '17 at 15:27
  • 1
    IT WORKS NOW THANKS FOR THE HINT! – Dawko Nov 23 '17 at 15:28

0 Answers0