Greetings again here. Some time ago I asked a question like this, but that time I solved it with an answer that they gave me and then I got through with that. But this time I need to find a way to do this. I need to process a registration form with Ajax and pass the data to my php. I'm using classes, methods, POO in short using PDO. The truth is that now if I have to find a way through my ajax, pass the data to a method of my class. It is a registration form and I want to pass the data directly to the method of my class from the url variable of my ajax. This is what I have done:
jQuery.ajax({
url:'Controllers/procesar_registro.php',//AQUI ES DONDE QUIERO PASARLE LOS DATOS AL METODO
type: 'POST',
dataType: 'json',
data: $(this).serialize(),
beforeSend: function(){
$('#enviar').val('Registrandote...');
$('#enviar').attr("disabled", "disabled");
}
})
.done(function(response){
console.log(response);
if(response.respuesta == false){
$('#enviar').val('Registrar');
$('#enviar').removeAttr("disabled");
$('#alerta_err').slideDown('slow');
setTimeout(function(){
$('#alerta_err').slideUp('slow');
},6000);
}else{
$('#alerta_ok').slideDown('slow');
setTimeout(function(){
$('#alerta_ok').slideUp('slow');
location.href = 'index.php';
},4000);
}
});
});
PHP
public function NuevoAdmin(){
if(isset($_POST['newAdmin']) && !empty($_POST)){
$name = $_POST['name']; $ape = $_POST['ape']; $ced = $_POST['ced'];
$id_estado = $_POST['estado']; $id_perfil = $_POST['id_perfil']; $username = $_POST['username'];
$pass = $_POST['pass']; $date = date('d-m-Y');
$obj->InsertAdmin($name,$ape,$ced,$username,$pass,$date,$id_perfil,$id_estado);
}else{
}
}
Then what I would like is something like this in my ajax
url: 'Controllers / Controllers.php / NewAdmin', which would be the name of the folder, the name of the file and the name of the method.
I am not sure if this can be done, but I have researched and seen on the internet that a person has done it the way I say but has used a constant echo URL / Name of the method, and it has worked. I've tried and I could not make it work, I do not know if it's because the way I saw was using an autoload or something like that.
I have also achieved this:
$(document).ready(function(){
//cuando hagamos submit al formulario con id id_del_formulario
//se procesara este script javascript
$("#id_del_formulario").submit(function(e){
e.preventDefault();
$.ajax({
url: $(this).attr("action"),//action del formulario, ej:
//http://localhost/mi_proyecto/mi_controlador/mi_funcion
type: $(this).attr("method"),//el método post o get del formulario
data: $(this).serialize(),//obtenemos todos los datos del formulario
error: function(){
//si hay un error mostramos un mensaje
},
success:function(data){
//hacemos algo cuando finalice todo correctamente
}
});
});
});
In this example, in the url, the action of the form passes, so would you give me to understand that my form could send it to my controller?
I would really like to get the way to do it, without first having to send the data to a file and then send it to my class method. Who can help me with this, I'll be grateful!