I have an angularjs service that sends a rest api link to a php file, the problem is that , this php file is calling another php file which contains a javascript code. When I execute the code in the service, it executes the php file , but the javascript code is just printed out without being executed. It's been a whole day of research but without any solution. Please can you tell me where the problem is? I have used require 'secondFile.php';
and include 'secondFile.php';
, I have also tried to bring the whole javascript code from the second file to the first file, so that I call the javascript locally, but it always prints the code without executing it.
Here is my code:
The angularjs service function:
var link = 'http://path/to/first.php';
$http.post(link, {idf : newDoc.idf})
.success(function (res){
console.log(res);
}).error(function (err) {
console.log(err);
});
the first php file, which is a remote file
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$method = $_SERVER['REQUEST_METHOD'];
//$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
$input = json_decode(file_get_contents('php://input'),true);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
var_dump($request);
$idf= $request->idf;
$val= $request->val;
switch ($method) {
case 'POST':
insertit($idf, $val);
break;
case 'PUT':
updateit();
break;
case 'REMOVE':
removeit();
break;
}
}
catch(PDOExecption $pe) {
echo "okay";
print "ERROR!".$pe->getMessage();
die();
}
function insertit($idf, $val) {
include 'connect.php';
$dataBilan = array($val);
$stmt = $DB->prepare("INSERT INTO `Test` (val) VALUES (?)");
$stmt->execute($dataBilan);
$last = $DB->lastInsertId();
$idf= $last;
$info_bilan= array(
'val' =>$val
);
$data = array(
'idf'=>$last,
'action'=>'update',
'table'=>'activite',
'data' =>$info_bilan
);
require 'second.php';
echo "okay done inserting";
}
and second.php:
<script src="pouchdb-5.3.1.min.js"></script><script type="text/javascript" language="javascript">
var dbRemote = new PouchDB('http://localhost:5984/Mydatabase');
var myDocs;
var value = <?php echo json_encode($data); ?>;
console.log(JSON.stringify(value));
if((value.idf != null) && (value.idf != 0))
{
console.log("-------here we go----");
dbRemote.allDocs({include_docs: true}).then(function (res) {
myDocs = res.rows.map(function (row) {
return row.doc; });
onUpdate(value);
});
}
}
function binarySearch(arr, docId) {
var low = 0, high = arr.length, mid;
while (low < high) {
mid = (low + high) >>> 1; // faster version of Math.floor((low + high) / 2)
arr[mid].id < docId ? low = mid + 1 : high = mid
}
return low;
}
function onUpdate(value)
{
console.log("couchdb's "+ myDocs.length);
if(myDocs.length !=0)
{
console.log("haha");
var index = binarySearch(myDocs, value.idf);
var doc = myDocs[index];
if(action== "update")
{
dbRemote.put(doc).then (function () {
console.log("Updated! ");
}).catch(function (err) {
console.log(err);
});
}
}
}
</script>
The content of second.php file is just printed out but it's not executed. When I execute first.php everything goes right , but when I execute the service I just see the printed file. Please any ideas?