Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate the fields from HTML form in Laravel?

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
like image 990
flash Avatar asked Aug 22 '18 20:08

flash


People also ask

Can we use HTML in laravel?

Laravel provides various in built tags to handle HTML forms easily and securely. All the major elements of HTML are generated using Laravel. To support this, we need to add HTML package to Laravel using composer.


3 Answers

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.

like image 176
user2094178 Avatar answered Oct 09 '22 19:10

user2094178


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.

like image 20
Rich Avatar answered Oct 09 '22 19:10

Rich


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.

like image 28
Veerendra Avatar answered Oct 09 '22 18:10

Veerendra