I have a class like below :
class Giving extends Controller
{
public function test()
{
$name; // i want to access $name from function below
$email; // i want to access $email from function below
return view('route/routes', [
'name' => $name,
'email' => $email
]);
}
public function sendGiving()
{
$name = $this->request->name;
$email = $this->request->email;
}
}
function sendGiving receives its value from ajax post (at blade view) :
data = {
_token: '{{ csrf_token() }}',
name: $('#txtName').val(),
email: $('#txtEmail').val(),
phone_number: $('#txtPhnum').val(),
}
$.ajax({
url: "{{ route('sendGiving') }}",
type: 'POST',
data: data,
dataType: 'JSON',
success: function (response) {
//do something
}
});
Routes :
Route::get('/givingtest','Giving@test')->name('test');
Route::post('/givingsend', 'Giving@sendGiving')->name('sendGiving');
how can i access value from sendGiving() function to test() function ? Or can i make a second ajax post call in the view to send data to test() function ?
Session. Store the values in session and get in the subsequent request$this->test();. In your code you want to access variable from functiontest()where the variable are$email&$name, but both are undefined. So you cant do that. I saw, you gonna access/givingtestvariable on route/givingsendin this case you need to send variable with form method from/givingtestto/givingsendroute. Then you can access from function.