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

UmaWorld's avatar

Laravel doesn't read id from a hidden input

is it true?

0 likes
17 replies
tykus's avatar

No. Can you share relevant code (e.g. the form markup and any Middleware/Controller logic)?

martinbean's avatar

@umaworld No, it’s not true. I use hidden inputs frequently.

Laravel is just a server-side framework. A hidden input transmits its value when a form is submitted. They would be pretty useless if they didn’t send their values.

1 like
Tray2's avatar

@jayandholariya because it's never sent to the server. A disabled element in a form is never included in the submitted form data.

jayandholariya's avatar

@martinbean I'm not asking about hidden inputs—I agree that they're commonly used and their values are captured when the form is submitted. My question is about disabled inputs, because their values aren't captured by the controller when the form is submitted.

jayandholariya's avatar

@Tray2 Is there any way to fetch the value of a disabled input? I need to make the input readonly at that point.

martinbean's avatar

I'm not asking about hidden inputs—I agree that they're commonly used and their values are captured when the form is submitted. My question is about disabled inputs, because their values aren't captured by the controller when the form is submitted.

@jayandholariya That’s right. Because they’re disabled.

Why are you disabling inputs if you actually want their values? And why are you hijacking @umaworld’s thread to ask about this, instead of creating your own thread with your question?

UmaWorld's avatar
<td>
			<a href=""
                        onclick="event.preventDefault();
                         document.getElementById('delete-id').value = '{{ $task->id }}';
                         document.querySelector('#delete-form').submit()"
                        class="btn btn-danger">Delete
			</a>
</td>
 <form action="{{ route('tasks.destroy',$task->id) }}" id="delete-form" class="hidden" method="POST">
 @csrf
    @method('DELETE')        
<input type="hidden" name="id" id="delete-id" value="{{ $task->id }}">
    </form>

pls check this

martinbean's avatar
Level 80

@UmaWorld How are you expecting that to work? Delete URIs have the ID in the URI itself; not passed as a form field. You should be changing the form action; not a hidden input’s value.

Put the task’s URI in the href of your link, and then change the deletion form’s action attribute to that value before submitting the form:

<table>
    <tbody>
        @foreach($tasks as $task)
            <tr>
                <td>
                    <a
                        href="{{ route('tasks.destroy', compact('task')) }}"
                        onclick="
                            event.preventDefault();
                            var form = document.getElemenyById('delete-task-form');
                            form.action = this.href;
                            form.submit();
                        "
                        role="button"
                    >
                        Delete
                    </a>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>
<form action="#" id="delete-task-form" method="post" style="display:none;">
    @csrf
    @method('delete')
    <button type="submit">Delete</button>
</form>

You should probably put some sort of confirmation in place before deleting resources as well.

jlrdw's avatar

@UmaWorld looks like you could use some javascript tutorials. Many lessons are here for free.

1 like
newbie360's avatar

@UmaWorld Without js

@foreach ($tasks as $task)
    <button type="submit" form="delete-form" formaction="{{ route('tasks.destroy', $task) }}">
        Delete {{ $task->id }}
    </button>
@endforeach


<form id="delete-form" method="POST">
    @csrf
    @method('DELETE')
</form>
brianobbo's avatar

Kindly debug the request: $task->I'd to check if it's not null

Please or to participate in this conversation.