P
P
Pavel2019-01-20 13:30:35
Laravel
Pavel, 2019-01-20 13:30:35

How to get the value of a field that is displayed through foreach?

How to get only one specific value?

<table class="table">
            @foreach ($files_info as $val)
                <tr>
                    <th>{{$num++}}</th>
                    <td>{{$val['filename']}}</td>
                    <td><input value="{{$val['filename']}}" name="del_file" type="text" hidden><button type="button">удалить</button></td>
                </tr>
            @endforeach
            </table>

So it always gets the value of the last field, which is displayed by the loop
public function del_file(Request $request)
    {
        $del_file_name = $request->input('del_file');

        dd($del_file_name);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Timur, 2019-01-21
@sidiqi

When asking a question, describe what you want to do.
In this case, you want to make a "delete" button for every row in the table, but you're doing it wrong.
You have one form for the entire table and many inputs with the same name del_file, so when processing you get only the last element.
You need a different form for each button

<table class="table">
    @foreach ($files_info as $val)
        <tr>
            <th>{{$num++}}</th>
            <td>{{$val['filename']}}</td>
            <td>
                <form action="" method="post">
                    <input value="{{$val['filename']}}" name="del_file" type="text" hidden><button type="button">удалить</button>
                </form>
            </td>
        </tr>
    @endforeach
</table>

M
Maxim Timofeev, 2019-01-20
@webinar

You all have the same name input, so it's not surprising that you get only 1. There should be an array, probably, like:
Then get an array of values

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question