How to transfer data from view (twig file) to controller - laravel

I'm using Laravel 5. I have a function in my Controller, which use some of my params. This params I save in my view, while the user enters them in the input. And I need this data to transfer to my controller. Is it possible? Maybe anybody has had the same problem, I hope You can help.
Thank You!
Here is my controller:
$databaseLogin = new DatabaseController();
$dbRequest = $databaseLogin->loginReq($login, $passwd);
return view('login');
And here is my view:
<input class="form-control input-lg" type="text" id="login" name="login" placeholder="Login">
<input type="password" name="passwd" id="pass" autocomplete="off" class="form-control input-lg" placeholder="Password">

You can do if from post request
1.First create a route
Route::POST('your route’, 'yourController#add');
2.Then create a form with csrf token in view
<form action="{{url(‘your route’)}}" method="post">
{{csrf_field()}}
<input type=”text” name=”username”>
<input type=”text” name=”password”>
<input type=”submit” value=”login”>
</form>
3.Then you can get the values in your controller
Public function add(Request $request){
$name = $request.username;
$password= $request.password;
//code continues
}

Related

Why isn't the thymeleaf object recognized?

The first code snipped is me adding(or rather trying) the attribute 'NewUser' to use in html.
The error pops up for 'th:field="*{name}"'.
Error:
Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'
#RequestMapping("home")
public String newUser(Model model) {
User newUser = new User();
newUser.setId(100l);
newUser.setName("Test");
newUser.setEmail("test#what.com");
newUser.setPermission(0);
model.addAttribute(newUser);
return "index";
}
<form action="#" th:action="#{/home/add}" th:object="${newUser}" method="get">
<p>Id: <input type="text" th:field="*{name}" /></p>
<p>Message: <input type="text" th:field="*{email}" /></p>
<p>Message: <input type="text" th:field="*{permission}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
I think the cause is you didn't create the getter method for the fileds: name, email, and permission. So, when the thymeleaf try to render the page, it cannot read the value of the name field, or email field, or permission field.
I was adding another model and it seems like it was causing issues, so I ended up doing this and everything is working fine.
The 'userList' model is used elsewhere in the code.
#GetMapping("home")
public String index(Model model) {
User user = new User();
model.addAttribute("user", user);
model.addAttribute("userList", userService.getUsers());
return "index";
}
<form th:object="${user}" th:action="#{/home/add}" method="get">
<p>Id: <input type="text" th:field="*{name}" /></p>
<p>Message: <input type="text" th:field="*{email}" /></p>
<p>Message: <input type="text" th:field="*{permission}" /></p>
<p><input type="submit" value="Submit" /> <input/></p>
</form>
maybe this will help someone...the issue is that you didn't provide the model that thymleaf will use to map your form input value. so you just have to pass as parameter "user model" like that .....
public String newUser(User model) {
}
.....thymleaf will use it to map your form field ...it work fine for me

Change PUT request to POST request to include image update

I wrote a post edit method which at first consisted only of text. I did the update using a PUT request.
Now I want to include images to my posts, so I added it, and it works for my POST request of creating a post but doesn't work for my update post, when I want to change image, since PUT request doesn't support file uploads.
So now I'm stuck trying to change my update method from PUT request that only updates text to a POST request that updates both the text and an image if supplied.
This is the code I wrote so far:
public function update(Request $request, Post $post)
{
//store updated image
if($request->hasFile('image') && $request->file('image')->isValid()){
if($post->hasMedia('posts')) {
$post->media()->delete();
}
$post->addMediaFromRequest('image')->toMediaCollection('post', 's3');
}
$post->update(request()->validate([
'body' => 'required'
]));
return redirect($post->path());
}
I think that the $post->update doesn't work for the POST request. I just want to update a text if an update was given.
Using Laravel 6.
EDIT: My form layout structure (simplified)
<form action="action="/posts/{post}" method="POST">
#method('PUT')
#csrf
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
My routes:
Route::get('/posts/{post}/edit', 'PostsController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostsController#update');
I tried your code and it worked fine with me , only added #csrf in form tag.
<form action="/posts/{post}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>

How to set a route to a controller and access it via a form? [duplicate]

This question already has answers here:
Laravel form post to controller
(2 answers)
Closed 3 years ago.
I'm trying to set a route to my "MessagesController" so that a i can access it from my form, How do i set a route to the controller and access it from action form ?
This is my Form code
<form action="#" id="ajax-contact" method="GET" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
What should i write inside my web.php to set a route so i can acces my MessageController.php what should i write inside the action attribute ?
Okay not sure what you want but here is the whole thing
REQUEST(FORM) => ROUTE => CONTROLLER => RESPONSE
So you want to go from FORM to Controller you have to :
Create a controller (in App\Http\Controllers) and a method
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
// This method will handle the form
// $request is where the form data will be
public function handleForm(Request $request){
// this means dump all form values
dd($request->all());
}
}
Create a route ( In simple words, this will link between FORM & Controller method ) in web.php
// The request need to be POST to /handle-data
Route::post('handle-data','MyController#handleForm');
finally this should be your form
<form action="/handle-data" id="ajax-contact" method="POST" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
If you want to make this as an ajax call
$( "#ajax-contact" ).submit(function( event ) {
event.preventDefault();
var data = $(this).serializeArray();
$.post('/handle-data', data ).then(function(response){
console.log(response);
});
});
if you want to test the ajax way, change the dd($request->all()); to return response()->json($request->all());
That should be it :)
Edit routes inside web.php to point to submit method in MessagesController:
Route::post('submit', 'MessagesController#submit')->name('submit');
then set form action to:
<form action="{{ route('submit') }}" id="ajax-contact" method="POST" enctype="multipart/form-data">
You should POST from a form.

How to count checked checkbox using laravel?

we can count checked value of checkbox using js, jquery, etc. But there is no available source that gives how to count it using Laravel
Form
<form action="" method="post">
#csrf
<input type="checkbox" name="hobbis[]" value="programming"> prog<br>
<input type="checkbox" name="hobbis[]" value="circket"> cricket<br>
<input type="checkbox" name="hobbis[]" value="football"> football<br>
<input type="submit" value="sub">
</form>
laravel
Route::post('/store', function (Request $req) {
return count($req->hobbis);
});

submit form can not access method in controller

When I submit form in view, I can't access that in controller.
View create.blade.php
#extends('layouts.app')
#section('content')
<form method="POST" action="posts">
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
Controller PostController
public function store(Request $request)
{
return $request->all();
}
I want to access the form data, in PostController in the store method and once I click submit, it not giving the result.
I have route: Route::resource('posts', 'PostController') and I am posting to this route/url, as action="posts". In Route I have used resource and in method="post"
Does it matter? What can I do?
Looks like you are missing the action
Route::post('post', 'PostController#store');
I'm not sure but may be it's because of url and not included csrf_field(). Try changing it to:
<form method="POST" action="{{ url('posts') }}">
{{ csrf_field() }}
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
or you may use route('posts.store ') instead of url('posts')

Resources