R
R
ross_viktor2019-10-19 15:46:43
PHP
ross_viktor, 2019-10-19 15:46:43

How can I get data from db in ajax in smarty template?

hello i have such loop in smarty template

<select name="qs_category" id="qs_category">
        <option value="">Категория</option>
        {foreach from=$categories item=v name=cat}
        <option value="{$v.id}" >{$v.str}{$v.name}</option>
        {/foreach}
      </select>


does anyone have a ready-made code that would take these values ​​not in the select, but would receive them in the input somehow via Ajax directly from the database? no.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2019-10-20
@glaphire

I will describe in general terms, because otherwise you need to reproduce the entire project:
1) Call the future input for autocompletion some kind of ID, for example.

<input id="first-level-category-autocomplete" type="text">

2) Attach jQuery UI autocomplete to it (in the example for the #users input, it seems to be the same), an example of sane js is here , there ajax is done in the background for you, you just need to add this code correctly to the smarty template and respond from the backend correct to send
3) on the handler that was specified for autocomplete, write your getAutocomplete method for categories (select *** where category like '$term%' ***), form a response in php with a string according to the format
[{label:"label1", value: "value1"},{label:"labelN", value: "valueN"}]
.
4) Check that everything is ok with the output of values ​​for autocomplete on the frontend (console.log and alert to help) 5) in the select
event of autocomplete, describe what you want to do after the user has selected a category from the proposed ones and it has been substituted into the input. Most likely here you want to load child categories with ajax. Write a normal ajax request to the URL inside the select event, in which you will find child categories, form html and send it to the front, receive it on the front in the success handler and append to the desired layout element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question