0

I am trying to submit a form using ajax in Laravel 5.5 The problem is the page is refreshing and not submitting data in the database. I need to store data in the database without refreshing the page.
Here is my code:

Controller

public function new_timing_table(Request $request)
{
    if (Request::ajax()) {
        $timing_tables =  new Timing_Table;
        $timing_tables->timing_tables_name = $request->timing_tables_name;
        $timing_tables->save();
        $msg = "yes";
    } else {
        $msg = "yes";
    }
    return ['msg'=> $msg];
}

View

<form id="timeForm" class="form-horizontal form-material" >                                                                      
  <div class="form-group">                                                                      
   {{ csrf_field() }}
   <div class="col-md-12 m-b-20">
   <label> Table Name</label>
   <input type="text" id="timing_tables_name" class="form-control" 
   name="timing_tables_name" />                                                                            
   </div>
   <div class="modal-footer">
   <input type="button" value="Replace Message" id='btnSelector'>
   </div>
  </div>
</form>

Ajax script

const xCsrfToken = "{{ csrf_token() }}";
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': xCsrfToken
    }
});


jQuery(document).ready(function() {
    jQuery('#btnSelector').click(function(e) {
        event.preventDefault();
        getMessage();
    });
});
var getMessage = function() {
    var timing_tables_name = $("input[name=timing_tables_name]").val();
    $.ajax({
        type: 'post',
        url: '/new_timing_table',
        dataType: 'json', //Make sure your returning data type dffine as json
        data: timing_tables_name,
        //data:'_token = <php echo csrf_token() ?>',
        success: function(data) {
            console.log(data); //Please share cosnole data
            if (data.msg) //Check the data.msg isset?
            {
                $("#msg").html(data.msg);
            }
        }
    });
}

Router

 Route::post('/new_timing_table','Timing_TableControoler@new_timing_table');
2
  • 3
    I am doing great, thanks for asking. :) On a more serious note: Greetings and salutations are not needed on SO, on contrary they often clutter the question and make it difficult to read. Please keep a good quality of your questions because future users of this site might search for similar problem and your question might help them find an answer too. Commented Dec 2, 2018 at 19:48
  • On your jquery use e.preventDefault(); you are passing e and expecting from event Commented Dec 2, 2018 at 19:58

2 Answers 2

1

You got a typo or a mistake in your script.

jQuery('#btnSelector').click(function(e){
    // An error here - it should be e.preventDefault();
    event.preventDefault();
    getMessage();
});
Sign up to request clarification or add additional context in comments.

2 Comments

What isn't working? I can't help you, when you just say that it's not working and leave it at that - give me specifics. What isn't working? What errors do you get?
Thank you for your help, I have solve the problem now as I have add it in the following replay
0

My code is working now after adding beforeSend: function (request) in Ajax script

var getMessage = function(){
var timing_tables_name = $("#timing_tables_name").val();
console.log(timing_tables_name);

$.ajax({
    type:'GET',
    url:'/new_timing_table', //Make sure your URL is correct
    dataType: 'json', //Make sure your returning data type dffine as json
     data: 
     {
         timing_tables_name
     },
    beforeSend: function (request) {
            return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf- 
     token']").attr('content'));
        },
    success:function(data){
        console.log(data); //Please share cosnole data
        if(data.msg) //Check the data.msg isset?
        {
            $("#msg").html(data.msg); //replace html by data.msg
        }

    }
});
 }

and editing the controller to be simple as this one

  public function new_timing_table(Request $request){
    $timing_tables =  new Timing_Table;
    $timing_tables->timing_tables_name = $request->timing_tables_name;
    $timing_tables->save();
     $msg = "This is a simple message.";
     return ['msg'=> $msg];
      }

Thank you all for your help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.