How to get Data from javascript call into presenter?

7 months ago

+
0
-

I have a jquery call to a presenter, which fetches data for an autocomplete/suggest in a form field.

<script>
$(function() {
        $( "#hint" ).autocomplete({
                source: "/recipes/admin/getingredients",
                minLength: 2
        });
});
</script>

In the presenter, I have this.

<?php
       public function actionGetIngredients( $name )
        {
                $data = $this->db->table('recipe_ingredient')->where('name LIKE ?', $name.'%')->fetchPairs('id', 'name');
                $this->sendJson( $data );
        }
?>

The issue is, that the variable $name is empty, or rather, null.
Everything works if I create a dummy array for name. eg: $name=[1=>‘foo’, 2=>‘bar’]
How do I get data, from the javascript, into the $name variable in the presenter?

7 months ago

Toanir
Member | 51
+
0
-

Hi, it seems like the autocomplete plugin does not send the search term in GET parameter name.

To debug this, you should open the network tab in your browsers developer tools and check the request in its parameters.

Even better, you should check the documentation of your JS plugin, if any exist. I believe you are using jqueryui autocomplete plugin whose documentation state that the parameter name should be term.

7 months ago

+
0
-

Aah, yes, changing the variable name to term works…
Now.. if I want 2 on the same page…

7 months ago

kamil_v
Member | 212
+
0
-

Use another HTML ID, 4 ex: “#hint” and “#another_hint”.

7 months ago

dkorpar
Backer | 94
+
+3
-

also never hardcode links like:
source: “/recipes/admin/getingredients”,

rather use
source: {plink :Recipes:Admin:getingredients},