Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maaz's avatar
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.

0 likes
3 replies
Tray2's avatar

Then share your solution for future references.

A tip though , never mix the http verbs like you are doing. In your form you use the method post but in your javascript you use get.

1 like

Please or to participate in this conversation.