Back

How To Solve if - else Statements are not working in Jquery


I am creating a Ticket Reservation System and I want to check data availability from the Database. As usual, I used HTML form and when someone tries to insert data which is already in the database , I want to show them a message like "Seats not available". And If data is unique , I want to insert into the database. I have a function called btnShowNew in Seats.blade.php and when I click submit button it always shows me " if " part in that function. It means , it always shows Seats not available. " else " part is not working. IT means , if data is unique , I am not able to insert into the database. ( Seat Number = Items )

Form and Seat Structure Image

How can I Fix this ??

Here is my Seats.blade.php

<form class="form-horizontal" id="form1" method="POST" action="{{ route('seatsinsert') }}" enctype="multipart/form-data">    
    
    {{ csrf_field() }}    
        
        <div class="dt"> <br>
           
    <h4> <span id="success_message" class="text-success"></span> </h4>
    
    <div class="form-group row">
    <label for="example-date-input" class="col-2 col-form-label">Select Date :</label>
    <div class="col-10">
    <input class="form-control" type="date" name="date" placeholder="mm-dd-yyyy" id="example-date-input">
    </div>
    </div>
        
     <div class="form-group">
    <label for="exampleSelect1">Select Time :</label>
    <select name="st" class="form-control" id="exampleSelect1">
      <option>10.30 am</option>
    </select>
    </div>  
 
        </div>
            
      <h2 style="font-size:1.2em;font-family: Times New Roman;"> Choose seats by clicking below seats :</h2>
       
      <div id="holder"> 
	<ul id="place">
        </ul>    
      </div>
      
      <div style="width:600px;text-align:center;overflow:auto"> <br>
      
      <input type="submit" class="btn btn-primary" id="btnShowNew" value="Continue"> <br><br>

       <span id="availability"></span>          
    
    <script type="text/javascript">
        
        $(function () {

            $('#btnShowNew').click(function (e) {
                e.preventDefault();

                var items = [];
                $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
                    items.push($(this).attr('title'));
                });

                    console.log(items);
                   // $(location).attr('href', 'Seats');

                   $.ajax({
                   url:'{{ route('seatprocess') }}',
                   type:"POST",
                   data:{ 
                   _token: "{{ csrf_token() }}", 
                   items: JSON.stringify(items), 
                   date: $('input[name=date]').val(), 
                   st: $('select[name=st]').val()},
                   success:function(data)
                   {
                    if(data !== '0')
                    {
                     $('#availability').html('<span class="text-danger">Seats not available</span>');
                     $('#btnShowNew').attr("disabled", true);
                    }
                    else
                    {
                     //$('#availability').html('<span class="text-success">Seats Available</span>');
                     
                    $.ajax({ 
                    type: "post", 
                    url: "{{ route('seatsinsert') }}", 
                    data: { 
                    _token: "{{ csrf_token() }}", 
                    items: JSON.stringify(items), 
                    date: $('input[name=date]').val(), 
                    st: $('select[name=st]').val()}, 
                    success: function(data){ 
                    $("form").trigger("reset"); 
                    $('#success_message').fadeIn().html("Text"); 
                    } 
                    });
                     
                    $('#btnShowNew').attr("disabled", false);
                    }
                   }
                  });
  
                }); //btnShowNew

         }); //Final 
    
    </script>
    
    </form>

Here is my SeatsController.php

public function seatsinsert(Request $request)
    {

        $date = $request->input('date');
        $st = $request->input('st');
        $items = $request->input('items'); 
        $items = str_replace(['[', ']', '"'], '', $items); 

        $user = new Seats(); 
        $user->date = $date; 
        $user->st = $st; 
        $user->item = $items; 

        $user->save();
  
    }  
    
    public function seatprocess(Request $request)
    {
    //dd($request->all());
    
    $date = $request->input('date');
    $st = $request->input('st');
    $items = $request->input('items');     
    
    $results = DB::table('seats')->where(['date' => $date, 'st' => $st, 'item' => $items])->get();

    echo $results;
    
    }
 }

Here are my Routes.

Route::post('seatsinsert',[
'uses'=> '[email protected]',
'as' => 'seatsinsert'
]);
Route::post('seatprocess',[
'uses'=> '[email protected]',
'as' => 'seatprocess'
]);

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us