I'm starting with native mvc php and I do not know how to pass the view data to the controller.
what is the right way to do it?
I'm starting with native mvc php and I do not know how to pass the view data to the controller.
what is the right way to do it?
You could use javascript with Ajax to do it
Example:
<script >
$(document).ready(function(){
//---Pasar Parámetros al Controlador -----
$("#Id_del_Botón").click(function(){
//los inputs o campos
var parametros = {
"nombre" : $("#nombre").val(), // #id_del_campo
"apellido" : $("#apellido").val(),
"operacion":"insertar_usuario"
//Este seria el nombre de tu funcion en el modelo, en este caso, "insertar usuario"
}
$.ajax({
data: parametros,
url: 'controlador_usuario.php', //nombre del archivo de tu controlador
type: 'post',
beforeSend: function () {
$("#modal_ingresar").modal();//LLAMOS AL MODAL QUE NOS MUESTRA EL MENSAJE, SI ES QUE TIENES ALGUNO
},
});
});
//---Fin de Pasar Parametros--------------
});
</script>
Controller
<?php
include_once 'modelo_usuario.php';
$objeto= new Usuario();
switch($_POST["operacion"]){
//insertar datos de los usuarios, pasando las variables
case "insertar_usuario":
$objeto->nombre=$_POST["nombre"];
$objeto->apellido=$_POST["apellido"];
$objeto->insertar_usuario();
break;
//de esta forma con cada función del modelo
case 'buscar':
$objeto->buscar=$_POST["buscar"];
$tabla=$objeto->buscar();
echo $tabla;
break;
}
?>
Model