How to get Data from javascript call into presenter?

+
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?

Toanir
Member | 57
+
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.

+
0
-

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

Kamil Valenta
Member | 820
+
0
-

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

dkorpar
Member | 136
+
+3
-

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

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