I am getting an parse error when I run the AJAX code for autocomplete. The response looks great. But I don’t why the error occurring.
Here is the Script and HTML code
<script type="text/javascript"><!-- $('input[name='filter_parent']').autocomplete({ 'source': function(request, response) { $.ajax({ url: 'index.php?route=catalog/category/autocompletecat&user_token={{ user_token }}&filter_parent=' + encodeURIComponent(request), dataType: 'json', success: function(json) { response($.map(json, function(item) { return { label: item['name'], value: item['category_id'] } })); }, beforeSend: function(jqXHR) {}, error: function(jqXHR, textStatus, errorThrown) {console.log(errorThrown);}, complete: function(jqXHR,status) {} }); }, 'select': function(item) { $('input[name='filter_parent']').val(''); } }); //--></script> <div id="filter-category" class="col-md-3 col-md-push-9 col-sm-12 hidden-sm hidden-xs"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"><i class="fa fa-filter"></i> {{ text_filter }}</h3> </div> <div class="panel-body"> <div class="form-group"> <label class="control-label" for="input-parent">{{ entry_parent }}</label> <input type="text" name="filter_parent" value="{{ filter_parent }}" placeholder="{{ entry_parent }}" id="input-parent" class="form-control" /> </div> <div class="form-group"> <label class="control-label" for="input-status">{{ entry_status }}</label> <select name="filter_status" id="input-status" class="form-control"> <option value=""></option> {% if filter_status == '1' %} <option value="1" selected="selected">{{ text_enabled }}</option> {% else %} <option value="1">{{ text_enabled }}</option> {% endif %} {% if filter_status == '0' %} <option value="0" selected="selected">{{ text_disabled }}</option> {% else %} <option value="0">{{ text_disabled }}</option> {% endif %} </select> </div> <div class="form-group text-right"> <button type="button" id="button-filter" class="btn btn-default"><i class="fa fa-filter"></i> {{ button_filter }}</button> </div> </div> </div> </div>
Here is the controller with the json response
public function autocompletecat() { $json = array(); $json[] = array( 'category_id' => 0, 'name' => strip_tags(html_entity_decode('---All---', ENT_QUOTES, 'UTF-8')) ); if (isset($this->request->get['filter_parent'])) { $this->load->model('catalog/category'); $filter_data = array( 'filter_parent' => $this->request->get['filter_parent'], 'sort' => 'name', 'order' => 'ASC', 'start' => 0, 'limit' => 5 ); $results = $this->model_catalog_category->getSuperParentCategories($filter_data); foreach ($results as $result) { $json[] = array( 'category_id' => $result['category_id'], 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')) ); } } $sort_order = array(); foreach ($json as $key => $value) { $sort_order[$key] = $value['name']; } array_multisort($sort_order, SORT_ASC, $json); $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } [{"category_id":0,"name":"---All---"}]
I tried with other functions, none of them are working. This is so mysterious, I don’t know what causes this error. Please help me on this. I almost spend the entire day for this.
Thanks in Advance
Advertisement
Answer
Finally found it. Error is causing because of user_token which is not declared in controller.
$data['user_token'] = $this->session->data['user_token'];