0

I'm trying to get data from my form in Laravel 8. In this form I try to write down information from user and send it by json. My forms looks like:

<form action="/profile" method="get" enctype="application/json">
    <label for="name">Player name</label>
    <input type="text" id="name" value="{{ $data->name }}">
    <label for="from">The lowest number</label>
    <input type="number" id="from" value="{{ $data->from }}">
    <label for="to">The highest number</label>
    <input type="number" id="to" value="{{ $data->to }}">
    <label for="attempts">Number of attempts</label>
    <input type="number" id="attempts" value="{{ $data->attempts }}">
    <button type="submit">Save</button>
</form>

I have Controller for this and for the action="/profile":

namespace App\Http\Controllers\Game;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $data = $request;
        
        return view('newUser', [
            'data' => $data
        ]);
    }

    public function profile(Request $request)
    {
        $data = $request->all();
        dd($request->all());

        return response($data)
            ->header('Content-Type', 'application/json');
    }
}

And my Routes:

Route::get('/new', 'UserController@store')
    ->name('user.store');

Route::get('/profile', 'UserController@profile')
    ->name('user.profile');

The problem is that I think i will get the values from this form, but the $request->all() is only empty array. I don't know what I'm missing. How to get this data?

Thanks for help.

4
  • Didn't you put a 'submit' button or are you submitting it with JS? Commented Jan 14, 2021 at 11:40
  • I had before button type="submit", but nothings changed. I added it know in my code and still the same, empty array. I will edit it in my question. Commented Jan 14, 2021 at 11:48
  • Did you include the csrf field in the form tag? Commented Jan 14, 2021 at 11:58
  • I added it now, forget about it. After that when I submit my form, I have generated token in my array, but still nothing else, still without any values from inputs. Commented Jan 14, 2021 at 12:13

1 Answer 1

2

You have to set 'name' attribute in your inputs:

<input type="text" id="name" name="name" value="{{ $data->name }}">
Sign up to request clarification or add additional context in comments.

Comments

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.