Привет, я пытаюсь изучить основы MVC, на данный момент я не использую oop, а просто процедурный, не использую фреймворк и не использую маршрутизатор.
Мне нужно отправить формуиспользуя ajax и jquery, проблема в том, что если я отправляю форму контроллеру, то ajax возвращает всю страницу, как я могу просто вернуть echo $ output;на вызов Ajax?,Я также использую контроллер для визуализации представлений, и это контроллер.Вы предлагаете использовать другой контроллер только для вызова ajax?
// Load settings files
require_once($_SERVER["DOCUMENT_ROOT"].'/config/load.php');
// Start session
session_start();
// Switch the view
switch ($_SESSION['km-user-session']['km-user-role']) {
case KM_ADMIN_ROLE:
ob_start();
// Load header for admin
include_once(KM_ROOT_PATH.'/km-views/km-admin/km-header.php');
$km_header = ob_get_contents();
ob_end_clean();
// Check if api is active and show different sidebar
if($_SESSION['km-user-session']['km-api-active'] == true){
ob_start();
include_once(KM_ROOT_PATH.'/km-views/km-admin/km-api/km-sidebar.php');
$km_sidebar = ob_get_contents();
ob_end_clean();
}else{
ob_start();
include_once(KM_ROOT_PATH.'/km-views/km-admin/km-sidebar.php');
$km_sidebar = ob_get_contents();
ob_end_clean();
}
// Load admin api receipts view
include_once(KM_ROOT_PATH.'/km-views/km-client/km-api/km-receipts.php');
break;
case KM_CLIENT_ROLE:
ob_start();
include_once(KM_ROOT_PATH.'/km-views/km-client/km-header.php');
$km_header = ob_get_contents();
ob_end_clean();
// Check if api is active and show different sidebar
if($_SESSION['km-user-session']['km-api-active'] == true){
ob_start();
include_once(KM_ROOT_PATH.'/km-views/km-client/km-api/km-sidebar.php');
$km_sidebar = ob_get_contents();
ob_end_clean();
}else{
ob_start();
include_once(KM_ROOT_PATH.'/km-views/km-client/km-sidebar.php');
$km_sidebar = ob_get_contents();
ob_end_clean();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];
$clientCode = $_SESSION['km-user-session']['km-api-user-code'];
$buildindCode = $_SESSION['km-user-session']['km-api-building-code'];
$dates= array(
'pk_prop' => $clientCode,
'pk_cnd' => $buildindCode,
'd_inizio' => $startDate,
'd_fine' => $endDate
);
// cURL request to the receipts API
$cURL = curl_init (KM_API);
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS, http_build_query($dates));
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($cURL);
if (curl_error($cURL)) {
// Redirect to error 500 page and die
header('Location: '.KM_ERROR_500);
exit();
}
curl_close($cURL);
echo $output;
}
ob_start();
include_once(KM_ROOT_PATH.'/km-views/km-footer.php');
$km_footer = ob_get_contents();
ob_end_clean();
// Load client api receipts view
include_once(KM_ROOT_PATH.'/km-views/km-client/km-api/km-receipts.php');
break;
}
Это мой скрипт jquery
$(document).ready(function() {
// Initialize air datepicker plugin
$('.air-datepicker').datepicker();
// Store form into variable
var form= $("#requestForm");
// Actions when form is submitted
$('#submitForm').click(function(e) {
// Ajax request
$.ajax({
type: "POST",
data: form.serialize(),
dataType:"html",
success: function(result){
// Reload the iframe with new content
document.getElementById('tableContainer').contentDocument.location.reload(true);
// Show the iframe
$('#tableContainer').css('display','block');
var $iframe = $('#tableContainer');
$iframe.ready(function() {
// append result to the iframe
$iframe.contents().find("body").append(result);
});
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
swal('Il server non risponde', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');
} else if (jqXHR.status == 404) {
swal('Errore 404', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');
} else if (jqXHR.status == 500) {
swal('Errore 500', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');
} else if (exception === 'parsererror') {
swal('Si è verificato un errore!', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');
} else if (exception === 'timeout') {
swal('Time Out', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');
} else if (exception === 'abort') {
swal('Richiesta Annullata', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');
}
}
}); // Fine ajax
e.preventDefault(); // Prevent form to be sent
}); // fine submit form
}); // fine document ready