0

I want to get model_id[] value in foreach by click submit but i get all from model_id[].

In my form:

<form id="modify" class="form-horizontal" method="POST" action="{{ route('modifymodel') }}"
      onsubmit="return confirm('Are you sure to do this?');">
    {!! csrf_field() !!}
    <table class="table-hover" width="100%" cellpadding="5" cellspacing="0">
        @foreach($data_model as $key=> $va)
            <tr>
                <td width="30px">{{++$key}}</td>
                <td width="280px">
                    <input type="hidden" id="model_id[]" name="model_id[]" value=" {{ $va->model_id }} ">
                    <input type="text" name="txt_depmo" id="txt_depmo" style="width: 280px;"
                           value="{{$va->model_name}}">
                </td>
                <td>
                    <input type="submit" class="btn btn-outline-primary" name="submit" id="submit" value="Updates">
                    <input type="submit" class="btn btn-outline-danger" name="submit" id="submit" value="Removes">
                </td>
            </tr>
        @endforeach
    </table>
</form>

My Controller:

public function modifymodel(Request $request){
   $modelidd=Input::get('model_id');
 }

but i got the result all array in model_id[].

["1","3","8","9"]

i want to get id from my button click

4
  • model_id[] is an array, so you get an array back. In your foreach you could open a new <form> for every item and change the name of your input to name="model_id". Commented Oct 25, 2017 at 8:43
  • @kerbholz so i need to change my form in foreach?? Commented Oct 25, 2017 at 8:45
  • Basically, put your @foreach line at the top of your code and @endforeach line at the bottom of your code Commented Oct 25, 2017 at 8:48
  • But when i put form in foreach when i click on submit no action Commented Oct 25, 2017 at 8:50

1 Answer 1

1

Make form inside the loop. I will make the form for every item.

        @foreach($data_model as $key=> $va)

         <form id="modify" class="form-horizontal" method="POST" action="{{ route('modifymodel') }}"
             onsubmit="return confirm('Are you sure to do this?');">
             {!! csrf_field() !!}
            <table class="table-hover" width="100%" cellpadding="5" cellspacing="0">
            <tr>
                <td width="30px">{{++$key}}</td>
                <td width="280px">
                    <input type="hidden" id="model_id[]" name="model_id[]" value=" {{ $va->model_id }} ">
                    <input type="text" name="txt_depmo" id="txt_depmo" style="width: 280px;"
                           value="{{$va->model_name}}">
                </td>
                <td>
                    <input type="submit" class="btn btn-outline-primary" name="submit" id="submit" value="Updates">
                    <input type="submit" class="btn btn-outline-danger" name="submit" id="submit" value="Removes">
                </td>
            </tr>
          </table>
         </form>

        @endforeach
Sign up to request clarification or add additional context in comments.

2 Comments

But when i put form in foreach when i click on submit no action
yes please edit your answer table is inside form i will mark it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.