Saving data from a form with a bootstrap modal

1

I have a modal with a form and when I give the button to save the data I have entered, it does not send me to the address / path that is defined in the action attribute of the form tag, and therefore it does not save data In the database, what would I have to do to send the data to the controller? Here the modal form:

<div class="modal fade modal-slide-in-right" aria-hidden="true" role="dialog"
 tabindex="-1" id="comentario-{{$lib->lib_no}}" >
<form class="form-horizontal" method="POST" action="{{ route('comenta.store') }}">
    <div class="modal-dialog" role="document">


        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">x</span>
                </button>
                <h4 class="modal-title">Nuevo Comentario</h4>

            </div>



                <div class="modal-body">
                    <input type="hidden" name="_token" value="{{csrf_token()}}">
                    <label for="cmt_titulo">Título</label>
                    <input type="text" name="cmt_titulo" placeholder="Título...">

                    <br>
                        <label for="cmt_texto">Escribe tu comentario</label>
                    <br>
                        <textarea cols="50" rows="10" name="cmt_texto" placeholder="Escribe tu comentario..."></textarea>
                    <br>
                        <input type="hidden" name="cmt_libro_no" value="{{$lib->lib_no}}">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-primary" >Guardar</button>
                </div>

        </div>

    </div>
</form>

and this is the controller's method:

public function store(ComentarioFormRequest $request){

    $comentario= new Comentario;
    $comentario->cmt_no=0;

    $fecha=Carbon::now('Europe/Madrid');
    $comentario->cmt_fecha=$fecha->toDateString();
    $comentario->cmt_titulo=$request->get('cmt_titulo');
    $comentario->cmt_texto=$request->get('cmt_texto');


    $comentario->cmt_cliente_id=auth()->user()->u_cliente_id;

    $comentario->cmt_libro_id=Libro::select('id_libro')->where('lib_no', $request->get('cmt_libro_no'));
    $comentario->cmt_activo=1;
    $comentario->save();

    $comentario2=Comentario::orderBy('id_comentario', 'desc')
        ->first();

    Comentario::where('id_comentario', $comentario2->id_comentario)
        ->update(['cmt_no'=>++$comentario2->id_comentario]);


    return Redirect::to('cliente/comenta');
}

Nothing comes to this form request:

 <?php

namespace libreir\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ComentarioFormRequest extends FormRequest
{
/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'cmt_no'=> 'integer',
        'cmt_fecha'=>'date',
        'cmt_titulo'=>'string|required',
        'cmt_texto'=>'string|required',
        'msj_cliente_id'=>'integer',
        'cmt_libro_id'=>'integer|required',
        'cmt_activo'=>'integer|max:1|min:0'

    ];
}

}

    
asked by Maria Rosa Cambero 25.02.2018 в 15:19
source

1 answer

1

A good idea this time is to create only one modal, if you had 1000 records that need a modal, it would not be practical to generate 1000 manners.

<div class="modal fade modal-slide-in-right" aria-hidden="true" role="dialog"
 tabindex="-1" id="comentario" >
  <form class="form-horizontal" method="POST" action="{{ route('comenta.store') }}">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">x</span>
                </button>
                <h4 class="modal-title">Nuevo Comentario</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" name="_token" value="{{csrf_token()}}">
                <label for="cmt_titulo">Título</label>
                <input type="text" name="cmt_titulo" placeholder="Título...">
                <label for="cmt_texto">Escribe tu comentario</label>
                <textarea cols="50" rows="10" name="cmt_texto" placeholder="Escribe tu comentario..."></textarea>
                <input type="hidden" name="cmt_libro_no" value="">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <button type="submit" class="btn btn-primary" >Guardar</button>
            </div>
        </div>
    </div>

The only thing that changes is that the id of the modal does not depend on the id of the book, and we eliminate the value of the field cmt_libro_no .

<a href="" data-target="#comentario"  data-toggle="modal" class="comment-button">
    <button type="button" class="btn btn-default"  data-norden="{{$lib->lib_no}}">Comentar</button></a>

Now all you have to do is capture the "click" that you do on any "Comment" button (add a class to that button) and get the value of the id of the book, which I see that you already have stored in the attribute data-norden . Once you have that value, you assign it to the value of the field cmt_libro_no .

I will propose a solution with jQuery, taking into account that you use Bootstrap:

$('.comment-button').click(function (e) {
    $("input[name='cmt_libro_no']").val(
        $(this).find('button').first().data('norden'));
});
    
answered by 26.02.2018 / 18:38
source