Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaster's avatar

Dynamic Forms - Add Remove Forms - Jquery

Could you please suggest me a tutorial on the above mentioned

0 likes
6 replies
zaster's avatar

@bobbybouwmann is it possible to use only the update function of the Controller and the edit view to add dynamic forms and update them

Snapey's avatar

Explain what you want to achieve?

zaster's avatar

@Snapey

This is something which i have done, but it's not working as i want yet

jobs.edit.blade.php

  @foreach ($job->despatches as $despatch)


    <div class="despatch-section col-sm-2">
  <br>
      <div class="form-group">
        <label class="col-form-label" for="inputDefault">Despatch</label>

      </div>

      <div class="form-group">
        <input type="text" class="form-control" name="despatch_no[]" id="inputDefault" placeholder="Despatch Number" value="{{ $despatch->id }}">

      </div>

      <div class="form-group">
        <input type="text" class="form-control" name="despatch_date[]" id="inputDefault" placeholder="Despatch Date" value="{{ $despatch->despatch_date }}">

      </div>

      <div class="form-group">
        <input type="text" class="form-control" name="despatch_qty[]" id="inputDefault" placeholder="Despatch Quantity" value="{{ $despatch->despatch_qty }}">
      </div>
    </div>

  @endforeach 

jquery part

<script>
    $(document).ready(function() {
      var i = 1;
            $('#add-despatch').click(function(){
            i++;
          $('<div class="despatch-section col-sm-2">'+
          '<br>'+
          '<div class="form-group">'+
          '<label class="col-form-label" for="inputDefault">Despatch</label>'+
          '</div>'+
          ' <div class="form-group">'+
          '<input type="text" class="form-control" name="despatch_no[]" id="inputDefault" placeholder="Despatch Number">'+
          '</div>'+
          '<div class="form-group">'+
          '<input type="text" class="form-control" name="despatch_date[]" id="inputDefault" placeholder="Despatch Date">'+
          '</div>'+
          '<div class="form-group">'+
          '<input type="text" class="form-control" name="despatch_qty[]" id="inputDefault" placeholder="Despatch Quantity">'+
          '</div>'+
          '</div>').appendTo('#container');

           });
      });        
</script>

JobController.php

public function update(Request $request, $id)
    {


      $job = Job::find($id);
      //$job->jobNumber = $request->jobNumber;
      $job->customer_name = $request->customer_name;
      $job->company_name = $request->company_name;
      $job->job_type = $request->job_type;
      $job->job_status = $request->job_status;
      $job->job_delivery_date = $request->job_delivery_date;
      $job->job_delivery_time = $request->job_delivery_time;
      $job->pk_pkl = $request->pk_pkl;

      //Assumes that the maximum number of despatches will be less than 5
      for ($i=0; $i <5; $i++){
      //check whether an existing dispatch no is there in the database
      if(jDespatch::where('id', $request->despatch_no[$i])->get()->count() ){
      //Update Record - If there is an existing record
      DB::table('j_despatches')
        ->where('job_id',$id)
        ->where('id',$request->despatch_no[$i])
        ->update(
        [
        'despatch_date' => $request->despatch_date[$i],
        'despatch_qty' => $request->despatch_qty[$i]
        ]);
        //dd($request);
        $job->save();
        return redirect()->route('job.edit', $job->id);

      }

      else {
//Insert Record
        DB::table('j_despatches')
            ->insert(
            [
            'id' => $request->despatch_no[$i],
            'despatch_date' => $request->despatch_date[$i],
            'despatch_qty' => $request->despatch_qty[$i],
            'job_id' => $id
            ]);

            $job->save();
            return redirect('job');
      }

    }


      $job->save();
      return redirect('job');

    }
bobbybouwmann's avatar

Well this is the way to go. You need to post everything as an array and insert it into the database. What is currently not working for you?

zaster's avatar

@bobbybouwmann

  1. I only can add one dynamic form to the database, others are not visible after i save them(Not added to the database)
  2. Not able to add around 3 dynamic forms at the same time etc(To the database)..

Update and Insert is mentioned inside JobController - update function I am not sure it is the right way to do this

Please or to participate in this conversation.