0

I need to pass all variable data to PHP(Laravel) controller, so as to proceed with other operations with those variables in controller. Here when I do as per below mentioned code, I get response "hello" as output in console. But how do I take input in controller.

Route (web.php)

Route::get('invoice_data_process', 'App\Http\Controllers\InvoiceController@invoiceDataProcess');

Javascript:

$(function() {
        $("#print_invoice").on("click", function(e) {
            e.preventDefault(); // stop submission
            const arr = $("#rowData [id^=product_id]")
                .map(function() {
                    const $qnty = $(this).closest("tr").find("[id^=product_qnty]")
                    return ({
                        [$(this).attr("id")]: $(this).val(),
                        [$qnty.attr("id")]: $qnty.val()
                    })
                })
                .get();
            var invoiceNo = $('#invoiceNo').val();
            var invoiceDate = $('#invoiceDate').val();
            var clientName = $('#clientName').val();
            var grandTotal = $('#grandtotal').val();
            console.log(arr)


            $.ajax(
                {
                    type: "POST", //HTTP POST Method
                    url: '{{ URL::to('/invoice_data_process') }}', // Controller/View
                    data: { //Passing data
                        'invoiceNo' : invoiceNo,
                        'invoiceDate' : invoiceDate,
                        'clientName' : clientName,
                        'grandTotal' : grandTotal,
                        'arr' : arr
                    },
                    success: function(response) {
                        console.log("hello");

                    },

                });

        });
    });

Controller.php

public function invoiceDataProcess(Request $request)
{
    $invoice_number = $request->get('invoiceNo');
    $invoice_number = $request->get('invoiceDate');
    $client_name = $request->get('ClientName');
    $grand_total = $request->get('grandTotal');
    $arr = $request->get('arr');

    print_r( $invoice_number );
    die;
}

Header

<head>
 <meta name="csrf-token" content="{{ csrf_token() }}">
</head>

Output (http://127.0.0.1:8000/invoice_data_process)

Blank Page

5
  • Follow this thread - stackoverflow.com/questions/26351085/… Commented Jun 23, 2021 at 12:31
  • 1
    You have the route defined as Route::get, but you're trying to POST to it. Won't work Commented Jun 23, 2021 at 12:31
  • @aynber I used POST as well, but shows error "The GET method is not supported for this route. Supported methods: POST." Commented Jun 23, 2021 at 12:32
  • Sounds like you're trying to use both POST and GET for the same route. You'll need to create a route for each method, or use Route::match(['get', 'post'] Commented Jun 23, 2021 at 12:34
  • Does this answer your question? Laravel passing data using ajax to controller Commented Jun 23, 2021 at 16:12

0

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.