How to populate the fields from HTML form in Laravel?
Asked Answered
M

8

8

I am working on building a form in which I want to populate the fields coming from form (which I have named posting.blade.php)

The controller which I have used for that is:

public function store(Request $request)
{
    $this->validate($request, [
    'name' => 'required',
    'email' => 'required|email',
    'number' => 'required',
    'city' => 'required',
    'post' => 'required'
    ]);

    Mail::send('emails.posting-message', [
    'msg'=> $request->message
    ], function($mail) use($request) {
        $mail->from($request->email, $request->name);
        $mail->to('[email protected]')->subject('Contact Message');
    });
    return redirect()->back()->with('flash_message', 'Thank you for your message');
}


Problem Statement:

The current controller doesn't return anything as in the line 'msg'=> $request->message there is no message in validate. But if I use

'msg'=> $request->name (It returns name)

I am wondering what changes I should make in the controller so that it return every field present in the validate.

I tried with this but its only returning the last value which is post.

       'msg'=> $request->name,
       'msg'=> $request->email,
       'msg'=> $request->number,
       'msg'=> $request->city,
       'msg'=> $request->post
Mathews answered 22/8, 2018 at 20:33 Comment(7)
Using the Old Input it's the best way to do it IMO. Since it's reinforced by Laravel itself. laravel.com/docs/5.6/requestsWotton
What IMO means ?Mathews
In my opinion. :)Wotton
To be honest I haven’t worked on Laravel that much. I am wondering if you can give me a pointer what changes I need to make in the codeMathews
Man, hear me out when I say this, ok? Laravel is one of the most elaborated Frameworks I've worked with. There are no shortcuts for it. You need to study it. I would recommend following the tutorial they suggest, in this one laravel-news.com/your-first-laravel-application there is a mention on usage of old() function by the middle of it. Invest time on it mate. It is totally worth it.Wotton
Do you have form field with name 'message' ???Encephalo
Try adding a rule for message ('message' => 'string');Nabataean
R
10

First, you need to add ->withInput() to your redirect:

return redirect()->back()->with('flash_message', 'Thank you for your message')->withInput();

This will flash all submitted form fields to the session.

After the redirect, to get the value of a form field with the name of title for example, you can use the old() helper, such as:

<input type="text" name="title" value="{{ old('title') }}">

You can also pass a second parameter to the helper:

<input type="text" name="title" value="{{ old('title', $post->title) }}">

You get the idea.

Roberts answered 22/8, 2018 at 21:45 Comment(1)
The OP is not asking about the flash message but asking about the message which is a field name in the contact form and not getting its value in the controllerAlicyclic
C
2

It sounds like what you are trying to do is take all of the inputs from a form submission and pass them to a Laravel blade template called posting-message that is being used to build an email.

If my understanding is correct, then you are almost there - this just requires you to pass more variables through to your email blade template. At the moment, you are just passing through one variable called msg.

So the Mail section of your controller becomes something like:

Mail::send('emails.posting-message', [
    'name'=> $request->name,
    'email'=> $request->email,
    'number'=> $request->number,
    'city'=> $request->city,
    'post'=> $request->post
], function($mail) use($request) {
    $mail->from($request->email, $request->name);
    $mail->to('[email protected]')->subject('Contact Message');
});

Then in your email blade template you have access to the variables like {{ $name }}, {{ $email }} and so on.

p.s. If you wanted to get all the inputs and put them in an array in one go in your controller, Laravel provides some ways of retrieving inputs in bulk, e.g.:

$allInputs = $request->all();

p.p.s. Consider doing more validation than just required for each of your form inputs, to ensure the data supplied from your user is what you are expecting and not malicious or incorrect.

Confessional answered 26/8, 2018 at 16:5 Comment(0)
A
2

First thing you need to check the field name in your html that its exact message no spaces or sort of misspelled.

Second thing Check all the post fields using $request->all(); And see whether you get the "message" index in the post array or not.

There is nothing like if you have not added the field in the validation so you will not get it in the POST.

But as you are working with contact form you should keep the "message" field mandatory and should add it in validation. That way you will always get some data in the message field.

Can you please post the html also in the question so everyone will get more idea about it.

For the other issue about sending the data you already getting to the mail template you can use the below approach

$msg = array('name' => $request->name,'email' => $request->email,'number' => $request->number,'city' => $request->city,'post' => $request->post,'message'=>$request->message);

You can than use the array like

Mail::send('emails.posting-message', [$msg    
    ], function($mail) use($request) {
        $mail->from($request->email, $request->name);
        $mail->to('[email protected]')->subject('Contact Message');
    });

Then you can easily access all the variables in the mail template to make it dynamic.

Alicyclic answered 30/8, 2018 at 12:27 Comment(0)
R
0

The snippet you have provided as below:

   'msg'=> $request->name,
   'msg'=> $request->email,
   'msg'=> $request->number,
   'msg'=> $request->city,
   'msg'=> $request->post

Indicates that the array key msg is being overwritten 4 times.

You would want the following approach instead:

    $msg['name'] = $request->name;
    $msg['email'] = $request->email;
    $msg['number'] = $request->number;
    $msg['city'] = $request->city;
    $msg['post'] = $request->post;
Roberts answered 28/8, 2018 at 17:8 Comment(0)
F
0

First of all, you don't need to use the back method with redirect, back() function will redirect users anyway.

return back()->with('key','Val');

Check this out: Old Input

As others said, To send query strings/Inputs to the cookies use withInput() function according to the documantions it will flash all current inputs:

return back()->with('key','Val')->withInput();

If you had seen Laravel's default register and login blades, You would have known that you could use old() helper to get previous values based on cookies and sessions.


   'msg'=> $request->name,
   'msg'=> $request->email,
   'msg'=> $request->number,
   'msg'=> $request->city,
   'msg'=> $request->post

To assign a value to an array there are some ways, But remember never override them like this!

You could use the auto indexing method, If you use $msg[] = $request->bar, It will index values from 0 to the length of your assignments

$data[] = $request->val1;
$data[] = $request->val2;
...

But also you could determine each element's key like this:

$data['name'] = $request->name;
$data['email'] = $request->email;
...

After all, if you want to send all Inputs as second paramter just use $request->all():

Mail::send('emails.posting-message', $request->all(), function($mail) use($request) {
        $mail->from($request->email, $request->name);
        $mail->to('[email protected]')->subject('Contact Message');
});
Farthingale answered 29/8, 2018 at 20:32 Comment(0)
B
0

I have face similar problem some time ago, and after several tries I changed fieldname from message to comments and it was the solution.

I suspect that message maybe a variable name used by the framework in sessions or something like that.

I hope it helps.

Beltane answered 30/8, 2018 at 1:7 Comment(0)
D
0

yo need to add the withInput() method in your returned response as

return redirect()->back()->with('flash_message', 'Thank you for your message')->withInput();

to keep input from one request during the next request you need to use old() helper. Check the documention here https://laravel.com/docs/5.6/requests#old-input

<input type="text" name="title" value="{{ old('username') }}">

Laravel will automatically redirect the user back to their previous location. In addition, all of the validation errors will automatically be flashed to the session.Check the docs here https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors. To display validation error message you can use like this

@foreach ($errors->all() as $error)
    <li>{{ $error }}</li>
@endforeach

or more specifically

<input type="text" name="title" value="{{ old('username') }}">
<span class="error">{{$error->title}}</span>
Dacoit answered 30/8, 2018 at 11:35 Comment(0)
P
0

As i understand it, you're asking how to send the inputs to the mail template.

try this;

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'number' => 'required',
        'city' => 'required',
        'post' => 'required'
    ]);

    Mail::send('emails.posting-message', $request->all(), function($mail) use($request) {
            $mail->from($request->email, $request->name);
            $mail->to('[email protected]')->subject('Contact Message');
    });
    return redirect()->back()->with('flash_message', 'Thank you for your message');
}

now in the emails/posting-message.blade.php you will have access to $name, $email, $number, $city, $post and any other input you sent in the request.


now if you want to recover the errors of the validation. use this

$validator = Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
]);
if ($validator->fails()) {
    //you can get the errors with $validator->errors. and redirect/mail as you want.
}
//here the inputs are valid.
Prescript answered 31/8, 2018 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.