mardi 24 avril 2018

Laravel: Two Actions in a Form - one with "on-change" one with a button?

in my Laravel App I have a Blade "Insert Project" where I want to dynamic load html data from Database when a DropDown changes. To be more concrete: I have several Project Categories in the Table "Cats" (id, name, code) - Each Category needs different fields to be added, therefore I have put the html code in the Table "Cats". I want to achieve the following:

If a user selects a Project Category the html code which is attached to the category should be shown.

My Code so far:

<form method="POST" action="">
                  @csrf
                  <form action="" method="post">
                    <label for="Cat">Kategorie</label>
                        <select class="form-control" name="cat_id" id="cat_id" data-parsley-required="true" onchange='this.form.submit()'>
                            @foreach ($cats as $sn)
                              {
                                  <option value=""></option>
                                  }
                            @endforeach
                        </select>
                      </form>
<div class="form-group row mb-0">
                        <div class="col-md-6 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                
                            </button>
                          </div>
                      </div>
                  </form>

In my Controller I have add:

  public function insertProject(Request $data) {

    return $data['cat_id'];

}

public function filter(Request $request)
{
  $cat_id = $request->input("cat_id");
  return $cat_id;
  $code = DB::table('cats')->where('id', '=', $cat_id);

  var_dump($code->code);
}

When I change the DropDown, then the function "insertProject" will be called and not the desired function "filter".

How can I achieve it to call the correct function, or how can I achieve it to call "insertProject" only when the button has been pressed?

Thank you for your advice and help.

Stefan



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire