3

I have Datatable in which I am getting all the records from database, now I have rendered a feedback button for all the the records. On click of the Feedback button a modal popup window open with option to submit the feedback for particular id. Now as I have rendered Feedback button not sure how to insert the feedback for particular id using ajax. In my database there is Feedback column which have no values as of now, want to insert the value submitted to be inserted in database.

Here is my code how I have rendered Feedback button for all the records.

{
                    
                    render:function(data, type, row){
                       return "<a href='#modal-lg5' data-toggle='modal' data-target='#modal-lg5' class='btn btn-warning btn-sm'>  Feedback </a>"+
            "<div class='modal fade' id='modal-lg5'>"+
                "<div class='modal-dialog modal-lg'>"+
                 "<div class='modal-content'>"+
                    "<div class='modal-header'>"+
                        "<h4 class='modal-title'>Submit Feedback</h4>"+
                        "<button type='button' class='close' data-dismiss='modal' aria-label='Close'>"+
                            "<span aria-hidden='true'>&times;</span>"+
                        "</button>"+
                    "</div>"+
              "<div class='modal-body'>"+
                  "<div class='row'>"+
                    "<div class='col'>"+
                    "<form id='contactForm'>"+
                      "<div class='form-group'>"+
                        "<label>Details:</label>"+
                "<input type='text' name='title' class='form-control' placeholder='details' required=''>"+
            "</div>"+
                      "</div>"+
                      "<button class='btn btn-success'>Submit Feedback</button>"+
                      "</div>"+
                      "</form>"+
                    "</div>"
                }     
              }

This is how I have written the ajax

$("#contactForm").on('submit',function(e){

        e.preventDefault();

        var Feedback = $("input[name=title]").val();
        var url = '{{ route('postinsert') }}';
        
        $.ajax({
           url:url,
           method:'POST',
           data:{
                  
                  Feedback:Feedback
                },
           success:function(response){
              if(response.success){
                  alert(response.message) //Message come from controller
              }else{
                  alert("Error")
              }
           },
           error:function(error){
              console.log(error)
           }
        });

and this is my controller code:

 public function ajaxRequestPost(Request $request)
    {

        \DB::table('memberdetails')->insert([
            'Feedback' => $request->Feedback, //This Code coming from ajax request
        ]);

        return response()->json(
            [
                'success' => true,
                'message' => 'Data inserted successfully'
            ]
        );
    }

As I am working on this for the first time not sure how I can achieve my desired output, any help to resolve would be really great.

prashant
  • 57
  • 1
  • 1
  • 15
  • Hi, do you get any error message from the backend or something like this ..? does your jquery code exist separatly on a file.js or it is written on file.blade.php ..? – Hicham O-Sfh Jun 24 '21 at 11:59
  • @HichamO-Sfh No error.. jquery code is written in file.blade.js only – prashant Jun 24 '21 at 12:42
  • another question : why are you injecting the modal's code (first snippet) using js .. why not writing it directly ..? I don't understand what are you needing by posting this question on stack overflow, please could say what is the problem.. – Hicham O-Sfh Jun 24 '21 at 12:48
  • @HichamO-Sfh I Want a table with all the details coming from database and I want custom dropdown filters as well to filter all the data and for every row is getting loaded with members data.. now on click of the id it should redirect to it's details page. When I am writing directly all the data is getting loaded but I am not able to create a redirect link to each id which should open the details page nor custom filters are working..! – prashant Jun 24 '21 at 13:03
  • So found a solution using render function so I resolved the issues mentioned above – prashant Jun 24 '21 at 13:04
  • Now for every row there should be a feedback button and on click of this modal window opens user need to submit the feedback and I want to store that in database. – prashant Jun 24 '21 at 13:05
  • I am using Yajra datatables ....@HichamO-Sfh if you want I can share my file.blade.php – prashant Jun 24 '21 at 13:07

0 Answers0