How to correct this Ajax Request in Laravel [closed]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to send ajax request to fetch data from controller when I change date. I am not able to write ajax code properly. My code is as under:



{!!Form::open(['action' => 'BookingsController@store', 'method' => 'POST'])!!}
@csrf


<input type="date" name="eventDate" id="eventDate" value="">


<select name="vehicleName" id="vehicleName">
<option disabled selected>Choose Vehicle...</option>
<?php foreach ($availableVehicles as $key => $vehicle): ?>
<option id="{{$vehicle->id}}">{{$vehicle->name}} </option>
<?php endforeach; ?>
</select>

{!!Form::close()!!}







$(document).ready(function(){
$(#eventDate).change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{eventDate},
success:function(result){
$('#vehicleName').val('');
}
});
});
});












share|improve this question















closed as off-topic by Robin Green, rene, Makyen, jww, EJoshuaS Nov 18 '18 at 4:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Robin Green, rene, EJoshuaS

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • What's the issue in AJAX request it is not sent or it is giving any error?

    – Kamal Paliwal
    Nov 17 '18 at 7:47











  • I couldn't code it properly.

    – Ali Anwar
    Nov 17 '18 at 8:21


















0















I am trying to send ajax request to fetch data from controller when I change date. I am not able to write ajax code properly. My code is as under:



{!!Form::open(['action' => 'BookingsController@store', 'method' => 'POST'])!!}
@csrf


<input type="date" name="eventDate" id="eventDate" value="">


<select name="vehicleName" id="vehicleName">
<option disabled selected>Choose Vehicle...</option>
<?php foreach ($availableVehicles as $key => $vehicle): ?>
<option id="{{$vehicle->id}}">{{$vehicle->name}} </option>
<?php endforeach; ?>
</select>

{!!Form::close()!!}







$(document).ready(function(){
$(#eventDate).change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{eventDate},
success:function(result){
$('#vehicleName').val('');
}
});
});
});












share|improve this question















closed as off-topic by Robin Green, rene, Makyen, jww, EJoshuaS Nov 18 '18 at 4:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Robin Green, rene, EJoshuaS

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • What's the issue in AJAX request it is not sent or it is giving any error?

    – Kamal Paliwal
    Nov 17 '18 at 7:47











  • I couldn't code it properly.

    – Ali Anwar
    Nov 17 '18 at 8:21














0












0








0








I am trying to send ajax request to fetch data from controller when I change date. I am not able to write ajax code properly. My code is as under:



{!!Form::open(['action' => 'BookingsController@store', 'method' => 'POST'])!!}
@csrf


<input type="date" name="eventDate" id="eventDate" value="">


<select name="vehicleName" id="vehicleName">
<option disabled selected>Choose Vehicle...</option>
<?php foreach ($availableVehicles as $key => $vehicle): ?>
<option id="{{$vehicle->id}}">{{$vehicle->name}} </option>
<?php endforeach; ?>
</select>

{!!Form::close()!!}







$(document).ready(function(){
$(#eventDate).change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{eventDate},
success:function(result){
$('#vehicleName').val('');
}
});
});
});












share|improve this question
















I am trying to send ajax request to fetch data from controller when I change date. I am not able to write ajax code properly. My code is as under:



{!!Form::open(['action' => 'BookingsController@store', 'method' => 'POST'])!!}
@csrf


<input type="date" name="eventDate" id="eventDate" value="">


<select name="vehicleName" id="vehicleName">
<option disabled selected>Choose Vehicle...</option>
<?php foreach ($availableVehicles as $key => $vehicle): ?>
<option id="{{$vehicle->id}}">{{$vehicle->name}} </option>
<?php endforeach; ?>
</select>

{!!Form::close()!!}







$(document).ready(function(){
$(#eventDate).change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{eventDate},
success:function(result){
$('#vehicleName').val('');
}
});
});
});








$(document).ready(function(){
$(#eventDate).change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{eventDate},
success:function(result){
$('#vehicleName').val('');
}
});
});
});





$(document).ready(function(){
$(#eventDate).change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{eventDate},
success:function(result){
$('#vehicleName').val('');
}
});
});
});






ajax laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 18:21









bobbyrne01

2,27174498




2,27174498










asked Nov 17 '18 at 7:30









Ali AnwarAli Anwar

196




196




closed as off-topic by Robin Green, rene, Makyen, jww, EJoshuaS Nov 18 '18 at 4:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Robin Green, rene, EJoshuaS

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Robin Green, rene, Makyen, jww, EJoshuaS Nov 18 '18 at 4:21


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Robin Green, rene, EJoshuaS

If this question can be reworded to fit the rules in the help center, please edit the question.













  • What's the issue in AJAX request it is not sent or it is giving any error?

    – Kamal Paliwal
    Nov 17 '18 at 7:47











  • I couldn't code it properly.

    – Ali Anwar
    Nov 17 '18 at 8:21



















  • What's the issue in AJAX request it is not sent or it is giving any error?

    – Kamal Paliwal
    Nov 17 '18 at 7:47











  • I couldn't code it properly.

    – Ali Anwar
    Nov 17 '18 at 8:21

















What's the issue in AJAX request it is not sent or it is giving any error?

– Kamal Paliwal
Nov 17 '18 at 7:47





What's the issue in AJAX request it is not sent or it is giving any error?

– Kamal Paliwal
Nov 17 '18 at 7:47













I couldn't code it properly.

– Ali Anwar
Nov 17 '18 at 8:21





I couldn't code it properly.

– Ali Anwar
Nov 17 '18 at 8:21












2 Answers
2






active

oldest

votes


















3














1) You need to add into your data attribute the token variable token. Because Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.



data{'_token': '{{ csrf_token() }}'},


2) You forgot to add quotes into your #eventDate function. Change this:



$(#eventDate).change(function(){


to this



$('#eventDate').change(function(){


3) My opinion is to create the ajax inside the .change() function. Here is a full code below:



$(document).ready(function(){
$('#eventDate').change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{'_token': '{{ csrf_token() }}','eventDate' : eventDate },
success:function(result){
$('#vehicleName').val('');
},
});
});
});


I hope it helped you






share|improve this answer


























  • I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

    – Ali Anwar
    Nov 17 '18 at 11:45






  • 1





    Sorry, I don't understand what you mean.

    – Nikolas Diakosavvas
    Nov 17 '18 at 12:06











  • I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

    – Ali Anwar
    Nov 17 '18 at 19:19






  • 2





    That's a clompletly different question, it would be nice if you accept his answer.

    – Carlos Mora
    Nov 17 '18 at 20:53






  • 1





    Yeah Ali, just accept the answer and ask another question. Thanks.

    – Nikolas Diakosavvas
    Nov 17 '18 at 22:47



















1














To avoid form being submitted you should add:



$(document).ready(function(){
$("#form" ).submit(function( event ) {
// to track it is working: console.log( "Handler for .submit() called." );
event.preventDefault();
});
});





share|improve this answer
























  • I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

    – Ali Anwar
    Nov 17 '18 at 22:58






  • 1





    As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

    – Carlos Mora
    Nov 18 '18 at 16:31


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














1) You need to add into your data attribute the token variable token. Because Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.



data{'_token': '{{ csrf_token() }}'},


2) You forgot to add quotes into your #eventDate function. Change this:



$(#eventDate).change(function(){


to this



$('#eventDate').change(function(){


3) My opinion is to create the ajax inside the .change() function. Here is a full code below:



$(document).ready(function(){
$('#eventDate').change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{'_token': '{{ csrf_token() }}','eventDate' : eventDate },
success:function(result){
$('#vehicleName').val('');
},
});
});
});


I hope it helped you






share|improve this answer


























  • I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

    – Ali Anwar
    Nov 17 '18 at 11:45






  • 1





    Sorry, I don't understand what you mean.

    – Nikolas Diakosavvas
    Nov 17 '18 at 12:06











  • I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

    – Ali Anwar
    Nov 17 '18 at 19:19






  • 2





    That's a clompletly different question, it would be nice if you accept his answer.

    – Carlos Mora
    Nov 17 '18 at 20:53






  • 1





    Yeah Ali, just accept the answer and ask another question. Thanks.

    – Nikolas Diakosavvas
    Nov 17 '18 at 22:47
















3














1) You need to add into your data attribute the token variable token. Because Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.



data{'_token': '{{ csrf_token() }}'},


2) You forgot to add quotes into your #eventDate function. Change this:



$(#eventDate).change(function(){


to this



$('#eventDate').change(function(){


3) My opinion is to create the ajax inside the .change() function. Here is a full code below:



$(document).ready(function(){
$('#eventDate').change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{'_token': '{{ csrf_token() }}','eventDate' : eventDate },
success:function(result){
$('#vehicleName').val('');
},
});
});
});


I hope it helped you






share|improve this answer


























  • I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

    – Ali Anwar
    Nov 17 '18 at 11:45






  • 1





    Sorry, I don't understand what you mean.

    – Nikolas Diakosavvas
    Nov 17 '18 at 12:06











  • I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

    – Ali Anwar
    Nov 17 '18 at 19:19






  • 2





    That's a clompletly different question, it would be nice if you accept his answer.

    – Carlos Mora
    Nov 17 '18 at 20:53






  • 1





    Yeah Ali, just accept the answer and ask another question. Thanks.

    – Nikolas Diakosavvas
    Nov 17 '18 at 22:47














3












3








3







1) You need to add into your data attribute the token variable token. Because Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.



data{'_token': '{{ csrf_token() }}'},


2) You forgot to add quotes into your #eventDate function. Change this:



$(#eventDate).change(function(){


to this



$('#eventDate').change(function(){


3) My opinion is to create the ajax inside the .change() function. Here is a full code below:



$(document).ready(function(){
$('#eventDate').change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{'_token': '{{ csrf_token() }}','eventDate' : eventDate },
success:function(result){
$('#vehicleName').val('');
},
});
});
});


I hope it helped you






share|improve this answer















1) You need to add into your data attribute the token variable token. Because Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.



data{'_token': '{{ csrf_token() }}'},


2) You forgot to add quotes into your #eventDate function. Change this:



$(#eventDate).change(function(){


to this



$('#eventDate').change(function(){


3) My opinion is to create the ajax inside the .change() function. Here is a full code below:



$(document).ready(function(){
$('#eventDate').change(function(){
var eventDate = $(this).val();

$.ajax({
url:"{{ route('booking.create') }}",
method:"GET",
data:{'_token': '{{ csrf_token() }}','eventDate' : eventDate },
success:function(result){
$('#vehicleName').val('');
},
});
});
});


I hope it helped you







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 11:20

























answered Nov 17 '18 at 10:58









Nikolas DiakosavvasNikolas Diakosavvas

3391314




3391314













  • I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

    – Ali Anwar
    Nov 17 '18 at 11:45






  • 1





    Sorry, I don't understand what you mean.

    – Nikolas Diakosavvas
    Nov 17 '18 at 12:06











  • I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

    – Ali Anwar
    Nov 17 '18 at 19:19






  • 2





    That's a clompletly different question, it would be nice if you accept his answer.

    – Carlos Mora
    Nov 17 '18 at 20:53






  • 1





    Yeah Ali, just accept the answer and ask another question. Thanks.

    – Nikolas Diakosavvas
    Nov 17 '18 at 22:47



















  • I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

    – Ali Anwar
    Nov 17 '18 at 11:45






  • 1





    Sorry, I don't understand what you mean.

    – Nikolas Diakosavvas
    Nov 17 '18 at 12:06











  • I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

    – Ali Anwar
    Nov 17 '18 at 19:19






  • 2





    That's a clompletly different question, it would be nice if you accept his answer.

    – Carlos Mora
    Nov 17 '18 at 20:53






  • 1





    Yeah Ali, just accept the answer and ask another question. Thanks.

    – Nikolas Diakosavvas
    Nov 17 '18 at 22:47

















I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

– Ali Anwar
Nov 17 '18 at 11:45





I just want to submit the $request value to control onchange. out will be automatically generated. How can I send?

– Ali Anwar
Nov 17 '18 at 11:45




1




1





Sorry, I don't understand what you mean.

– Nikolas Diakosavvas
Nov 17 '18 at 12:06





Sorry, I don't understand what you mean.

– Nikolas Diakosavvas
Nov 17 '18 at 12:06













I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

– Ali Anwar
Nov 17 '18 at 19:19





I have $eventDate variable in the controller. I want to pass that value without submitting the form. for instance I ve a resource controller . In that controller I ve public function create(){} and public function store().. What I want I want to get the data of $availableVehicles from create(). If I submit the form it will go to store().

– Ali Anwar
Nov 17 '18 at 19:19




2




2





That's a clompletly different question, it would be nice if you accept his answer.

– Carlos Mora
Nov 17 '18 at 20:53





That's a clompletly different question, it would be nice if you accept his answer.

– Carlos Mora
Nov 17 '18 at 20:53




1




1





Yeah Ali, just accept the answer and ask another question. Thanks.

– Nikolas Diakosavvas
Nov 17 '18 at 22:47





Yeah Ali, just accept the answer and ask another question. Thanks.

– Nikolas Diakosavvas
Nov 17 '18 at 22:47













1














To avoid form being submitted you should add:



$(document).ready(function(){
$("#form" ).submit(function( event ) {
// to track it is working: console.log( "Handler for .submit() called." );
event.preventDefault();
});
});





share|improve this answer
























  • I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

    – Ali Anwar
    Nov 17 '18 at 22:58






  • 1





    As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

    – Carlos Mora
    Nov 18 '18 at 16:31
















1














To avoid form being submitted you should add:



$(document).ready(function(){
$("#form" ).submit(function( event ) {
// to track it is working: console.log( "Handler for .submit() called." );
event.preventDefault();
});
});





share|improve this answer
























  • I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

    – Ali Anwar
    Nov 17 '18 at 22:58






  • 1





    As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

    – Carlos Mora
    Nov 18 '18 at 16:31














1












1








1







To avoid form being submitted you should add:



$(document).ready(function(){
$("#form" ).submit(function( event ) {
// to track it is working: console.log( "Handler for .submit() called." );
event.preventDefault();
});
});





share|improve this answer













To avoid form being submitted you should add:



$(document).ready(function(){
$("#form" ).submit(function( event ) {
// to track it is working: console.log( "Handler for .submit() called." );
event.preventDefault();
});
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 '18 at 20:59









Carlos MoraCarlos Mora

9081920




9081920













  • I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

    – Ali Anwar
    Nov 17 '18 at 22:58






  • 1





    As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

    – Carlos Mora
    Nov 18 '18 at 16:31



















  • I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

    – Ali Anwar
    Nov 17 '18 at 22:58






  • 1





    As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

    – Carlos Mora
    Nov 18 '18 at 16:31

















I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

– Ali Anwar
Nov 17 '18 at 22:58





I have accepted his answer. Should I used it with Ajax request? Actually I am not understanding how to pass the value of this #eventDate to controller@create.. After loading form I have to select date from form that value will be sent to the controller for querry variable. After that the values accordingly change into the form like depend drop downs but here I' ve different tables from which I ll bring values here. After that I will submit the form that $request will be made on controller@store. I hope you got my question

– Ali Anwar
Nov 17 '18 at 22:58




1




1





As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

– Carlos Mora
Nov 18 '18 at 16:31





As I understand, you need 2 different requests: 1 for refill select items based on #eventDate and 2) after a car has been selected, to send the whole form to controller@store. For the first one, Nikolas answer is the right one. For the second, you have to add a 'submit' button and on its click, build a request with full data.

– Carlos Mora
Nov 18 '18 at 16:31



Popular posts from this blog

Bressuire

Vorschmack

Quarantine