How to get submitted data in the same form in laravel?

Member

by daisha , in category: PHP Frameworks , 19 days ago

How to get submitted data in the same form in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 18 days ago

@daisha 

In Laravel, you can access the submitted data in the same form using the old() function.


Here's an example of how to use it:

  1. In your Laravel Blade template file (usually located in the resources/views directory), you can use the old() function to access the submitted data in the same form.
  2. In your form fields, you can set the default value to the submitted value using the old() function. For example:
1
<input type="text" name="username" value="{{ old('username') }}" />


This will populate the input field with the submitted value if there is one, otherwise it will be empty.

  1. You can also use the old() function to populate other types of form fields, such as dropdowns, checkboxes, and radio buttons. For example:


Dropdown:

1
2
3
4
<select name="role">
    <option value="admin" {{ old('role') == 'admin' ? 'selected' : '' }}>Admin</option>
    <option value="user" {{ old('role') == 'user' ? 'selected' : '' }}>User</option>
</select>


Checkbox:

1
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }} />


Radio button:

1
2
<input type="radio" name="gender" value="male" {{ old('gender') == 'male' ? 'checked' : '' }} /> Male
<input type="radio" name="gender" value="female" {{ old('gender') == 'female' ? 'checked' : '' }} /> Female


By using the old() function, you can ensure that the submitted data is displayed in the same form after a validation error or when you want to display the previously submitted data.