Mark
Mark

Reputation: 622

Link PHP with Ajax Magento 2

I'm trying to create a simple "Register your interest" action. I have the front end working but I'm not sure how to link the AJAX to the PHP file. Where do I put the PHP file?

My current code for the AJAX is:

$.ajax({
                        url: "register-interest.php",
                        type: "GET",
                        dataType: "json",
                        data: {
                            type : "registerInterest",
                            email : userEmailLog,
                            user : userNameLog,
                            product : productTitle,
                            sku : productSku
                        },
                        success: function (response) {
                            JSON.stringify(response);
                        },
                        error: function (err) {
                            JSON.stringify(err);
                        },
                        complete : function() {
                            //$('.user-accept').addClass('unhide');
                            loading();
                        }

                    });

Upvotes: 1

Views: 2399

Answers (2)

Benjamin
Benjamin

Reputation: 1231

Your url value is:

"register-interest.php"

This implies that the php file that processes the AJAX request needs to be available at the same directory level as the page that includes the javascript file that is performing the AJAX request.

Example:

If your page is at http://example.com/my/ajax/page.html

Then the javascript will perform the AJAX request to the URL http://example.com/my/ajax/register-interest.php

Alternatively if you change the JS url value to read "/register-interest.php", then the AJAX request will be made to: http://example.com/register-interest.php

Where you need to put it on your server depends on how your web server's webroot's folder structure is organised, but you should be able to work back from the URL the javascript will be requesting to work this out.

Upvotes: 1

user6545788
user6545788

Reputation:

First you check url variable is correct or not and then provide response any data like

success: function (response) {
                        JSON.stringify(response);
                    },

Upvotes: 1

Related Questions