Answer the question
In order to leave comments, you need to log in
Ajax on mvc (php)?
Comrades, hello! In this question, a beginner, so do not kick)
How can I screw AJAX if I use the MVC template in php?
The project structure is below in the photo:
in the controller I have a view variable that checks the get parameter, for example, like this:
localhost/project/?view=profile
In general, I have one entry point. There is such a structure in index.php
In this project I already use ajax, but the whole page is sent and I cut out the piece I need from it, but now I need the "correct response" from the server, not the whole page...
Many thanks in advance to everyone!
include $view."php";
Answer the question
In order to leave comments, you need to log in
Project Structure
app\
____Controller\
________Main.php
public\
____js\
________ scriptr.js
vendor\
index.php
composer.json
.htaccess
In script.js file
$('#confirmForm').on('click', function (event) {
$.ajax({
url: '/confirm',
method: 'post',
dataType: 'json',
success: function (data) {
console.log(data);
}
});
});
<?php
session_start();
require_once '/vendor/autoload.php';
$router = new AltoRouter();
$router->map('GET|POST', '/confirm', 'Main::index');
if ($match = $router->match()) {
$path = explode('::', $match['target']);
$controllerName = 'Controller\\' . $path[0];
$controller = new $controllerName;
call_user_func_array([$controller, $path[1]], $match['params']);
} else {
header('Location: /404');
}
<?php
class Main()
{
public function index()
{
$service = new MainService();
$service->saveForm();
json_encode([
'success' => true,
'message' => 'Форма успешно сохранена'
], JSON_UNESCAPED_UNICODE);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question