Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to pass an ajax variable to a database

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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%201/200/myemail@gmail.com/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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading