Home > Software design >  I'm trying to pass an ajax variable to a database
I'm trying to pass an ajax variable to a database

Time:04-27

I am not familiar with AJAX but I have to use it for this project. Now here is where the problem lies. I am using a payment gate way and I have been able to run the code succesfully but I am having problem with fetching the reference number into the database I created. Below is the part of the code where I think the issue lies.

 callback: function(response){
                            let reference= response.reference;
                            //Verify payment using jqajax
                            $.ajax({
                                type: "GET",
                                url: "{{ URL::to('verify-payment') }}/" reference,
                                success: function (response){
                                    console.log(response);
                                    if(response[0].status == true){

                                        var name = $('#FirstName').val();
                                        var lastname = $('#LastName').val();
                                        var class = $('#class').val();
                                        var amount = $('#amount').val();
                                        var email = $('#email').val();



                                        $.ajaxSetup({
                                            headers: {
                                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                            }
                                        });
                                        //ajax request send

                                        $.ajax({


                                        type: 'GET',
                                        url: 'save-data/' name '/' lastname '/' class '/' amount '/' email '/' reference,
                                        success: function(result){

                                        $('form').prepend('<h4 >Payment successful</h4>').css("textAlign", "center");

                                        document.getElementById("paymentForm").reset();
                                        }
                                        });

I am trying to fetch the refrence number and pass into a column in my database. Here is the controller aspect that handles storing data.

public function save_data($name, $lastname, $classes, $amount, $email, $reference)
{

    //
    $paymentData = new Payment();
    $paymentData->First_name = $name;
    $paymentData->Last_name = $lastname;
    $paymentData->Class = $class;
    $paymentData->Amount = $amount;
    $paymentData->Email = $email;
    $paymentData->reference->$reference;
   
    $paymentData->save();
    return view('');
}

Here is the route I created for it.

Route::get('save-data/{name}/{lastname}/{class}/{amount}/{email}/{reference}', [PaymentController::class, 'save_data'])->name('save-data');

Do note that when I run the code this is what I get on the console.

jquery.min.js:2          GET http://127.0.0.1:8000/save-data/lucy/cage/J 1/200/[email protected]/775387417 500 (Internal Server Error)

I could see that the reference number was fetched but I don't know why it won't save into the database.

CodePudding user response:

I think you have problem in function save_date(...) in $paymentData->reference->$reference; should be $paymentData->reference = $reference;

  • Related