solved ;)
Dec 13, 2020
3
Level 2
Ajax call returning null result on query.
hey everyone. I have a table charters where there is a column charter_code . now i want when the form is submitted it should first check the charter_code inside charters table , whether it is available or not. here is my controller
public function getCharterCode(Request $request)
{
// dd($request->all());
$charter_code = $request->code;
$query = Charter::where('charter_code',$charter_code)->first();
dd($query);
return response()->json($query);
}
when i enter charter code which is avail in the database. but it is returning null.
blade
<form action="" method="post" id="checkout_form">
<input type="text" name="name" placeholder="First Name*" required value="{{$yacht->name}}">
<input type="text" name="l_name" placeholder="Last Name*" required >
<input type="text" name="email" placeholder="Email" required value="{{$yacht->email}}">
<input type="number" name="amount" placeholder="Amount (Euro)*" required >
<input type="text" name="charter_code" placeholder="Charter Code*" required id="charter_code">
<textarea type="text" placeholder="Subject*" required></textarea>
<button type="submit">Send</button>
</form>
ajax.js
<script>
$('#checkout_form').on('submit',function(e){
e.preventDefault();
let form = $('#checkout_form');
let charter_code = $('#charter_code').val();
$.ajax({
url: '/charter-code',
type: 'GET',
data:{
code: charter_code,
},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
})
</script>
routes
Route::get('/charter-code','Payment\PaymentController@getCharterCode');
Note:: this is not post request first i am checking if the charter code is available than do the post request otherwise not.
Please or to participate in this conversation.