Answer the question
In order to leave comments, you need to log in
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>
$("#form").on("submit", function(){
$.ajax({
url: 'непанятна',
method: 'post',
dataType: 'html',
data: $(this).serialize(),
success: function(data){
$('#message').html(data);
}
});
});
$res = '';
foreach($_POST as $key => $value) {
$res += $key.' : '.$value;
}
foo($res);
Answer the question
In order to leave comments, you need to log in
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
catalog/controller/extension/module/НАЗВАНИЕ_МОДУЛЯ.php
$("#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
class ControllerExtensionModuleНазваниеМодуля extends Controller {
public function index() {
//...
}
public function НазваниеМетода() {
//как то обрабатываете и возвращаете в json формате...
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
catalog/language/ru-ru/extension/module/НАЗВАНИЕ_МОДУЛЯ.php
admin/view/template/extension/module/НАЗВАНИЕ_МОДУЛЯ.twig
admin/controller/extension/module/НАЗВАНИЕ_МОДУЛЯ.php
admin/language/ru-ru/extension/module/НАЗВАНИЕ_МОДУЛЯ.php
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question