mercredi 7 novembre 2018

How to insert/post data in laravel using ajax into Database by query builder method without using model

This is my form inside Bootstrap modal.

<!-- Start BS Modal- Add Data Form -->
    <div id="studentModal" class="modal fade" role="dialog">
        <div class="modal-dialog modal-md">
          <div class="modal-content">
            <form method="POST" id="student_form" >
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                  <h4 class="modal-title">Add New Student</h4>
                </div>
                <div class="modal-body">
                  
                  <span id="form_output"></span>
                  <div class="form-group">
                    <label>First Name</label>
                    <input type="text" name="first_name" id="first_name" class="form-control" placeholder="Enter Your First name">
                  </div>
                  <div class="form-group">
                    <label>Last Name</label>
                    <input type="text" name="last_name" id="last_name" class="form-control" placeholder="Enter Your Last name" >
                  </div>             
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="button_action" id="button_action" value="insert" />
                    <input type="submit" name="submit" id="action" class="btn btn-success pull-left" value="Add" >
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button> 
                </div>
            </form>
          </div>
        </div>
    </div>
<!-- End - BS Modal -->

Add New is a button with id=add_data, when I click this button. It shows me the bootstrap modal having form. The JavaScript for this is below:

$('#add_data').click(function(){
            $('#studentModal').modal('show');
            $('#student_form')[0].reset();
            $('#form_output').html('');
            $('#button_action').val('insert');
            $('#action').val('Add');
        });

and when I click on submit button which has id=action as you can see in the input tag of form's code. The Javascript for this is given below: //Insert - script

$('#student_form').on('submit', function(event){
            event.preventDefault();
            var form_data = $(this).serialize();
            $.ajax({
                type:"ajax",
                method:"post",
                url:"",
                data:form_data,
                dataType:"json",
                success:function(data){
                    alert("Success");
                },
                error: function(data){
                    alert("Error");
                }
            })
        });

How to get form input values here in JS with csrf token? What should be in route? What should be in controller?

I used simple query in controller to read/get data from database which is like;

public function readData(){
    $student = DB::Table('user')->get();
    return response($student);
}

I need simple code like this I used for getting data from database.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire