I have a php function and in this php function I am trying to implement an API call using jQuery. I could not use PHP for this because of some restrictions so I need to use jQuery in this function. I added my jquery code inside PHP function which is working correctly for static data. But I do not know how I should pass PHP variables in data. Any suggestion regarding this will be appreciated.
Here is my code for jQuery
var settings = {
"url": "MY_URL_TO_CREATE_USER_ACCOUNT",
"method": "POST",
"timeout": 0,
"headers": {
"external-token": "MY_TOKEN",
"Content-Type": ["application/json", "text/plain"]
},
"data": "{rn"UserName": "user6",rn"FirstName": "F_NAME",rn"Password": "user6_pass",rn"LastName": "L_NAME",rn"Subscription": "Full",rn"ExpireDate": "2020-11-08"rn}",
};
jQuery.ajax(settings).done(function (response) {
console.log(response);
});
This code works for static data but I do not know how to pass php variables for the data attribute.
3
Answers
More proper solution would be using form.data for this.
Probably not the best solution, but try this,
Try this. Store the value of your PHP variable in a field then get the value of the field via jQuery.
Pass
PHP
variable toSCRIPT
by echo