I basically need to click a button in my blade view that triggers a function in my js file, that then sends some values to a controller and back to the the view. Using the code below, do I need to be creating a route for this? Thanks
view:
<div id="sendAsEmailTest">SEND AS EMAIL TEST</div>
js:
$('#sendAsEmailTest').on('click', function () {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "FormController/TestFunction",
data: { name: "John" }
}).done(function( msg ) {
// alert( "Data Saved: " + msg.name );
});
});
FormController:
public function TestFunction(Request $request)
{
return view('calculator.calculator',
[
'testdata' => $request,
]
);
}
thanks for the help @ßãlãjî. had to adjust things slightly, but seems to be working fine now:
js:
$('#sendAsEmailTest').on('click', function () {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "/formControllertestfunction",
data: { name: "John" }
}).done(function( msg ) {
// alert( "Data Saved: " + msg.name );
});
});
route:
Route::post('formControllertestfunction','FormController@TestFunction')->name('formControllertestfunction');
controller:
public function TestFunction(Request $request) {
return view('calculator.back',
[
'displayName' => $request->name ,
]);
}
your route should be post
route::post('formController/testfunction'@'FormController@TestFunction')
just
public function TestFunction(Request $request)
{
dd($request->name)<---- you get your attribute value here
}
then correct
js:
$('#sendAsEmailTest').on('click', function () {
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "formController/testfunction",<------------------all letters in small
data: { name: "John" }
}).done(function( msg ) {
// alert( "Data Saved: " + msg.name );
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.