D
D
Daniil Vershinin2020-03-14 01:24:54
opencart
Daniil Vershinin, 2020-03-14 01:24:54

Correct mvc ajax form processing module?

Task: there is a form whose data needs to be passed via Ajax for further processing. The question is how to correctly implement the functionality of such a module (how to correctly shove the specified code, from the point of view of mvc - what url to indicate in the Ajax request, because, as I understand it, just putting the handler in the root of the site is a crutch). No db calls are needed.
The form:

<form id="form">
  <input type="text" name="name">
  <input type="text" name="surname">
        <input type="submit">
</form>

Ajax form processing request:
$("#form").on("submit", function(){
  $.ajax({
    url: 'непанятна',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#message').html(data);
    }
  });
});

The code below, as I understand it, can be pushed into the view (in particular, 'catalog\view\theme\default\template\module\myModul.tpl')
Handler:
$res = '';
foreach($_POST as $key => $value) { 
$res += $key.' :  '.$value;
 } 
foo($res);


PS I would be grateful if you tell me where you can see the material where you can see the implementation of the "helloworld" module (roughly speaking, an empty module), along with registering the controller and displaying it in the admin panel, because in this task the necessary functionality is to enable / enable the module and add it to sidebar or footer of the site.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lazuren, 2020-03-17
@Titamik

That's right, you need to format your solution as a module.
All HTML (twig or tpl) that belongs to the front-end and JS code (in your case ajax) should be stored in:

catalog/view/theme/default/template/extension/module/НАЗВАНИЕ_МОДУЛЯ.tpl

Your Ajax should call the controller method which should be stored in:
catalog/controller/extension/module/НАЗВАНИЕ_МОДУЛЯ.php

and it should look something like this:
AJAX
$("#form").on("submit", function(){
  $.ajax({
    url: 'index.php?route=extension/module/НАЗВАНИЕ_МОДУЛЯ/НазваниеМетода&параметр=...',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#message').html(data);
    }
  });
});

PHP
<?php
class ControllerExtensionModuleНазваниеМодуля extends Controller {
    public function index() {
        //...
    }
    public function НазваниеМетода() {
        
        //как то обрабатываете и возвращаете в json формате...

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
}

Also, don't forget about language files:
catalog/language/ru-ru/extension/module/НАЗВАНИЕ_МОДУЛЯ.php

And all the same for the admin panel:
admin/view/template/extension/module/НАЗВАНИЕ_МОДУЛЯ.twig

admin/controller/extension/module/НАЗВАНИЕ_МОДУЛЯ.php

admin/language/ru-ru/extension/module/НАЗВАНИЕ_МОДУЛЯ.php

Read about creating modules here or in general about customizing OpenCart here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question