It is the first time that I try to do a method POST
with fetch
(I have not done it with either ajax
), I was seeing some codes but the truth is that I got lost more than I thought.
What I am wanting to do is send data from 2 input
that are in the form (modal) to the controller, but without reloading the page.
BLADE
<form role="form" class="form-horizontal" action="{{route("add_notas")}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="modal fade" id="AddNotas" tabindex="-1" role="dialog" aria-labelledby="AddNotasLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AddNotasLabel">Agregar una nota</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="recipient-name" class="col-form-label">Titulo:</label>
<input type="text" class="form-control-custom" id="recipient-name" name="titulo" required>
</div>
<div class="form-group">
<label for="descrp-text" class="col-form-label">Descripción:</label>
<textarea class="form-control-custom" id="descrp-text" name="concepto" required></textarea>
</div>
</div>
<div class="modal-footer d-flex justify-content-between">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><i class="fas fa-times"></i> cerrar</button>
<button type="submit" class="btn btn-success" id="enviar"><i class="fas fa-check"></i> Guardar</button>
</div>
</div>
</div>
</div>
JS
let boton = document.getElementById("enviar");
boton.addEventListener("click", function(e){
e.preventDefault();
var titulo = document.getElementById("recipient-name").value;
var concepto = document.getElementById("descrp-text").value;
var datos_form = {titulo: titulo, concepto: concepto, };
fetch('{{route("add_notas")}}',{
method: 'post',
body: JSON.stringify(datos_form)
headers:{'Content-Type': 'application/json'}
});
});
ROUTE
Route::post('/dashboard/agregar/notas/post','ControladorNotas@add_notas')->name('add_notas');
CONTROLLER
public function add_notas(Request $datos){
DB::table('tabla_notas')->insert([
'titulo' => $datos->titulo;,
'concepto' => $datos->concepto;
]);
return 'ok';
}
- Normally that's what I do when I make a
POST
. - The
return
always redirect to.Blade
in this case I don't know if I have to return the sameBlade
to another side or use some methodJSON
etc... as I commented at the beginning the first time doing this.
EDIT:
clarifying the problem.
- It doesn't save me in the
BDD
. - The page is reloaded (I don't want that).
First you should remove the attribute
action
from your form, sincefetch
you are already indicating the route to which your request is going to point.Later, the object that you pass as the second argument to the function
fetch
also accepts a key that is called:headers
which in turn is another object and within which we will indicate the passing of the tokencsrf
that is declared in your form and that must travel in the request so that laravel considers it trustworthy and processes it.Finally we will use the FormData constructor to collect and send the data by the AJAX request
Since the fetch API works or is built on promises, you must access the answers that it offers you, whether it was solved or if there was a network problem.
data.exito
we read the response from the server, accessing the keyexito
that we declared in the Laravel backendTest your request like this:
By the way, at your controller level, the response would be built like this:
So that when you process the request you can retrieve the key
exito
and paint the message associated with it in the view