jeudi 29 août 2019

How to save multiple form dynamic data with single submit button without reload the page in laravel?

I want to save multiple form with one submit button, without refresh the page, however, I have done with the help of jquery and javascript. When submitting the data, it's showing error.

Here is controller

              public function store1(Request $request)
{
    $this->validate($request, [
        'title' =>'required',

        'event_video_link'=>'required',
        'event_place_name'=>'required',
        'speaker_name'=>'required',
        'speaker_position'=>'required',
        'event_session_name'=>'required',
        'session_person_name'=>'required',
        'session_person_topic'=>'required',

        'session_time'=>'required',
        'event_date'=>'required',


        'session_person_pdf'=>'mimes:jpeg,pdf,docx,png:5000',
        'Image'=>'mimes:jpeg,pdf,docx,png:5000',
        'Featured'=>'mimes:jpeg,pdf,docx,png:5000',
        'session_person_image'=>'mimes:jpeg,pdf,docx,png:5000',
        'category_id'=>'required',
    ]);



    // Create Initial Required Data Array
    $data = [
        'title'=>$request->title,
        'event_video_link'=>$request->event_video_link,
        'event_place_name'=>$request->event_place_name,
        'speaker_name'=>$request->speaker_name,
        'speaker_position'=>$request->speaker_position,
        'event_session_name'=>$request->event_session_name,
        'session_person_name'=>$request->session_person_name,
        'session_person_topic'=>$request->session_person_topic,



        'category_id'=>$request->category_id,
        'slug'=>str_slug($request->title),

        // $date = date('Y-m-d H:i:s'),
        //  $date_time = Carbon::createFromFormat('Y-m-d H:i:s', $date)
        // ->format('d-m-Y'),
        'session_time'=>Carbon::parse(strtotime($request->session_time))->format('Y-m-d'),
        'event_date'=>Carbon::parse(strtotime($request->event_date))->format('Y-m-d'),


    ];


    if($request->has('title'))
    {
        $data['title']=$request->title;
    }

    if($request->has('event_video_link'))
    {
        $data['event_video_link']=$request->event_video_link;
    }


    if($request->has('event_place_name'))
    {
        $data['event_place_name']=$request->event_place_name;
    }




    if($request->has('speaker_name'))
    {
        $data['speaker_name']=$request->speaker_name;
    }

    if($request->has('speaker_position'))
    {
        $data['speaker_position']=$request->speaker_position;
    }





    if($request->has('event_session_name'))
    {
        $data['event_session_name'] = $request->event_session_name;

    }

    if($request->has('session_person_name'))
    {
        $data['session_person_name'] = $request->session_person_name;

    }

    if($request->has('session_person_topic'))
    {
        $data['session_person_topic'] = $request->session_person_topic;

    }



    if (request('File'))
    {
        $File = request('File');
        $file_name = time() . $File->getClientOriginalName();
        $File->move('uploads/past', $file_name);
        $data['File'] = 'uploads/past/'.$file_name;
    }

    // Optionally add 'featured' if found to the Data array
    if (request('Featured'))
    {
        $Featured = request('Featured');
        $file_name = time() . $Featured->getClientOriginalName();
        $Featured->move('uploads/past', $file_name);
        $data['Featured'] = 'uploads/past/'.$file_name;
    }
    // Optionally add 'file' if found to the Data array
    if (request('Featured1')) {
        $Featured1 = request('Featured1');
        $file_name = time() . $Featured1->getClientOriginalName();
        $Featured1->move('uploads/past', $file_name);
        $data['Featured1'] = 'uploads/past/'.$file_name;
    }
    if (request('Image')) {
        $Image = request('Image');
        $file_name = time() . $Image->getClientOriginalName();
        $Image->move('uploads/past', $file_name);
        $data['Image'] = 'uploads/past/'.$file_name;
    }


    // Create the Post with the $data Array

    past_events::create($data);
    //$post=Post::whereBetween('created_at',[$date_time, $to])->get();
    Session::flash('success', 'New Blog has been Published on Website for 
    Particular Menu');
    return redirect()->back();
    }

In create.blade.php, I have taken few field to show the column name

        <form  id="1sform" action="" 
        method="post"  enctype="multipart/form-data" class="form- 
        bg1" >
        
        <div class="input-group control-group increment" >
        <div class="form-group">
        <label class="control-label"> Person Name</label>
        <input type="text" name="speker[]" class="form-control" 
        placeholder="" >
        <small class="form-control-feedback"> </small>
        </div>
        </div>

        <div class="clone hide">
        <div class="control-group input-group" style="margin-top:10px">
        <label class="control-label">Person Name</label>
        <input type="text" name="speker[]" class="form-control" placeholder="" >
        <small class="form-control-feedback"> </small>
        </div>
        </form>

        <form  id="2ndform" action="" method="post"  enctype="multipart/form-data" class="form-bg1" >
        

        <div class="input-group control-group increment" >
        <div class="form-group">
        <label class="control-label">Gamer Name</label>
        <input type="text" name="gamer[]" class="form-control" placeholder="" >
        <small class="form-control-feedback"> </small>
        </div>
        </div>

        <div class="clone hide">
        <div class="control-group input-group" style="margin-top:10px">
        <label class="control-label">Gamer Name</label>
        <input type="text" name="gamer[]" class="form-control" placeholder="" >
        <small class="form-control-feedback"> </small>
        </div>
        </form>



        <form  id="3rdform" action="" method="post"  enctype="multipart/form-data" class="form-bg1" >
        

        <div class="input-group control-group increment" >
        <div class="form-group">
        <label class="control-label">runner Name</label>
        <input type="text" name="runner[]" class="form-control" placeholder="" >
        <small class="form-control-feedback"> </small>
        </div>
        </div>

        <div class="clone hide">
        <div class="control-group input-group" style="margin-top:10px">
        <label class="control-label">runner Name</label>
        <input type="text" name="runner[]" class="form-control" placeholder="" >
        <small class="form-control-feedback"> </small>
        </div>
        </form>

and here is script

$('.form-bg1').submit(function () { alert($(this).attr("id")); return true; }) $("#clickMe").click(function () { $(".form-bg1").trigger('submit'); // should show 3 alerts (one for each form) });
<script type="text/javascript">

    $(document).ready(function() {

        $(".btn-outline-success").click(function(){
            var html = $(".clone").html();
            $(".increment").after(html);
        });

        $("body").on("click",".btn-danger",function(){
            $(this).parents(".control-group").remove();
        });

    });

</script>
<script type="text/javascript">

    $(document).ready(function() {

        $(".btn-skype").click(function(){
            var html = $(".clones").html();
            $(".incremental").after(html);
        });

        $("body").on("click",".btn-danger",function(){
            $(this).parents(".control-groups").remove();
        });

    });

</script>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire