Home » Javascript » Sending request direct from PHP works but calling the PHP from AJAX to send request doesn't

Sending request direct from PHP works but calling the PHP from AJAX to send request doesn't

Posted by: admin August 19, 2018 Leave a comment


I want to send an ajax call to a PHP file, in which I have Javascript code to send data to SurveyJS (https://surveyjs.io/).

Calling ajaxOriginator.html using “localhost/testSurvey/ajaxOriginator.html” successfully calls receiver.php and the JavaScript in receiver.php runs but does not send out the request to SurveyJS.

If I call receiver.php directly using “localhost/testSurvey/receiver.php”, it successfully sends out the request to SurveyJS and I can see the data received.

Here is ajaxOriginator.html

<html lang="en">
<meta charset="utf-8"/>
<title>Ajax originator</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

$.get('receiver.php', { "name": "Julia", "car": "Kia" });


This is receiver.php

$name = $_GET["name"];
$car = $_GET["car"];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://surveyjs.azureedge.net/1.0.16/survey.jquery.min.js"></script>
var json = {};

window.survey = new Survey.Model(json);

//To test if ajax call works and it does
var values = ["<?php echo $name; ?>", "<?php echo $car; ?>"];

//To send data directly to SurveyJS
values = ["John", "somecar"];

var deepSurveyCopy = $.extend(true, {}, survey);
deepSurveyCopy.data.name = values[0];   
deepSurveyCopy.data.car = values[1];    
deepSurveyCopy.sendResult(my access key to surveyJS);

echo "Welcome ". $name. "<br />";
echo "You want ". $car;

Here are screenshots from Chrome and SurveyJS:
AjaxOriginator.html to receiver.php
Calling receiver.php directly
Data received from calling receiver.php directly

I am not very well-versed in PHP and would appreciate any help in understanding how all these work (and not work) together.

Basically ajaxOriginator.html is to simulate a plugin which is going to send data out to receiver.php, in which I am trying to wrap the data received in a format SurveyJS accepts and resend to SurveyJS.

Any help is much appreciated. Thanks!