Link PHP with Ajax Magento 2 - php

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();
}
});

First you check url variable is correct or not and then provide response any data like
success: function (response) {
JSON.stringify(response);
},

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.

Related

Self-POST AJAX request

I am having some issues running an AJAX request to the php class that contains the AJAX request. The AJAX request is called used a button, and works partially.
My AJAX request is held in a header.php file, which is included in my kpi2.php. If I specify the "url" for the AJAX call to a test file (in the same directory) the POST is successful and I can see the output. I was under the impression if I removed the "url" option it would indeed post to the same page, what am I doing wrong here?
function executeRefresh(){
if (control){
$(".loader").show();
$.ajax({
type: "POST",
data: { refresh: '1' },
success: function(json) {
if(!json.error) location.reload(true);
$(".loader").hide();
}
});
}
}
Output when no url is specified (meaning it should be posted to the same file that is calling the AJAX)
Written from /home/kpi/pages/kpi2.phpArray
(
)
This is the output when I have the option url: "test.php" (which has the exact same output but just in a different file.
Written from /home/kpi/pages/test.phpArray
(
[0] => refresh
)
EDIT:
To obtain the output generated from above, a simple export.
$v1 = print_r(array_keys($_POST),true);
$fp = fopen('../data/output.json', 'w');
fwrite($fp, 'Written from /home/kpi/pages/test.php'.$v1);
fclose($fp);
As for the control variable, it is just a simple listener that is true/false depending if the control key is clicked. It does indeed work and I've never had issues with it before.
As for the location.reload() I tried removing it and now it seems that it isn't even writing the php code now.
Originally in the header.php I had a button on the navbar that would call the AJAX function thus having the kpi2.php (which included the header.php) to have something posted to it. Once it was posted it would then call another php class.
Instead what I did was get rid of the onclick=executeRefresh() and then just listened to the button press specifically in the kpi2.php class. This way I could instantly execute the AJAX request to the other class without the un-needed post request to the class.
$(function() {
$(".btnRefresh").click( function()
{
if (control){
$(".loader").show();
$.ajax({
url:"../setup/kpi_quality.php",
type: "GET",
data: { casp: 'AVANT-CAP-321' }, // name of the post variable ($_POST['id'])
success: function(json) {
//if(!json.error) location.reload(true);
$(".loader").hide();
}
});
}
}
);
});

Deleting item using Ajax request with DELETE , using JQuery

I am trying to delete a post when clicking the (x) icon using Ajax request
here is my html code relating to this part :
I copied the JS and php parts from google , I've never written an Ajax request before
so I cannot figure out where I did wrong, please help
It seems that you have a problem with the right location of the php file.
It seems to be located on /static/delete.php, but you are calling /post/delete.php.
Other than that, you are not (at the moment) using DELETE, but POST.
Some frameworks use the POST as the method, but require some extra field (like _method on Laravel) to be present on the form, as some (if not all) HTTP servers do not accept methods other than GET or POST
$('#btnDeleteEmployees').click(function () {
$.ajax({
url: '/api/employees' + empID,
method: 'DELETE',
headers: {
"Content-Type": "application/json"
},
success: function (res) {
},
error: function (res) {
}
});
});

no data from callback, jquery ajax json, php, amazon

I've been trying the code at: https://gist.github.com/Exeu/4674423 this is a code that connects to amazon wsdl and retrieve product information. so far I have not been able to make it work. The code consist of a index.html, ama_funtion.js, search.php, AmazonECS.class.php. The index file loads and you insert the item name you want choose the category and other params, then when you click in the search link it goes to the ama_function.js and it make and ajax request to the search.php which have the passwords and keys to connect to the wsld, then it request the info from Amazon and return it as JSON to ama_function.js.
I have test ama_funtions.js to see if the index.html file connects to it, and it does, I did put an alert to show the input in the search box at index.html. Also I have test the search.php, I have manualy put the values in the search.php file and it connects to amazon and retrieve the product information as JSON object. the problem is that there is not a success callback as no data return to ama_funtions.js from search.php to be rendered. I know this as I tested it by adding an error function
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
error: function () {
alert("NO DATA");
},
and it did show me the alert, also I have tried to change the ->
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
for ->
data: {
q : value,
category : cat,
country : country,
page : page }
nothing works I am crazy looking to solve this, I will appreciate someone that help us with this. Please see a working example which belong the person sharing the code -> http://amazonecs.pixel-web.org. This is the way is suppose to work but it doesn't.
Regards
$.ajax({
url: "search.php",
dataType: 'json',
type: 'GET',
error: function () {
alert("NO DATA");
},
data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
data: {
q : value,
category : cat,
country : country,
page : page
}
})

jQuery $.ajax POST function into WPSC

I am using Wordpress Shopping cart. I want to append or modify the product name based on 3 variations the same thing goes for the price to.
I have tried to implement a simple Jquery on click event to an image using the AJAX $.post method within the single product page. It's not sending the data value even if I encode it with JSON. I must say that I'm new with JSON and PHP. Thanks in advance !
In the single product page
$(function() {
$('#div_img_link').click(function() {
$.ajax({
type: 'POST',
data: "test value",
// dataType : "json",
url: '../wpsc-cart_widget.php',
cache:false
});
});
});
In wpsc-cart_widget.php
$name=wpsc_cart_item_name();
if (isset($_POST['data'])){
$value1 = $_POST['data'];
}else{
$value1 = "";
}
echo "$name $value1";
I know that are some similar posts out there I've read them all can't figure it out. I want to know if the WPSC can perform the jQuery POST function without refreshing the page. Any help will be greatly appreciated!!
schematics
EDIT
SCHEMATICS 2 - to be more concise !
What I'm trying to accomplish
SEA, you're missing a function to handle the response that comes back from the server.
I would expect to see something like this :
$(function() {
$('#div_img_link').click(function() {
$.ajax({
url: '../wpsc-cart_widget.php',
type: 'POST',
data: "test value",
dataType : "JSON",
cache:false,
success: function(data) {
//do something here with the data.
//eg.
alert(data);
},
error: function() {
//do something here if an error occurs.
//eg.
alert("an error occurred");
},
});
});
});
The best place to learn more about ajax is the ajax() page in the jQuery API documentation.
You also need to ensure that the server response and the client expectation are compatible. With a client expectation of dataType: "JSON", you must ensure that the server returns a json-encoded response. Json-encoding is only necessary for multiple data in a single response. A single data item can be sent unencoded.
EDIT:
After seeing your Schematic 2, I think your ajax request data should look something like this :
data: {
'pid': '12345678',
'quantity': '1'
),
where pid is the "product id" and quantity is how many of the product the user wants to add to his/her basket.
But surely the WPSC documentation includes examples of this stuff? You shouldn't have to work it out for yourself.

How can I implement a jquery ajax form which requests information from a web api via a php request?

I'm trying to implement a simple api request to the SEOmoz linkscape api. It works if I only use the php file but I want to do an ajax request using jquery to make it faster and more user friendly. Here is the javascript code I'm trying to use:
$(document).ready(function(){
$('#submit').click(function() {
var url=$('#url').val();
$.ajax({
type: "POST",
url: "api_sample.php",
data: url,
cache: false,
success: function(html){
$("#results").append(html);
}
});
});
});
And here is the part of the php file where it takes the value:
$objectURL = $_POST['url'];
I've been working on it all day and can't seem to find the answer... I know the php code works as it returns a valid json object and displays correctly when I do it that way. I just can't get the ajax to show anything at all!!
...data: 'url='+encodeURIComponent(url),

Categories