impregnable fiend impregnable fiend - 1 year ago 36
jQuery Question

Execute func in PHP depends on what's sent by Angular

met another challenge inspecting web development, will be grateful if anyone can help. So... I've got a few buttons each have to execute some func, like that

ng-click='loadA', ng-click='loadB'
etc. I'm making request like

$scope.loadA = function() {
$http({
method: 'POST',
url: 'generate.php',
data: {keyword : 'A'},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function(data) {
console.log(data);
})
};


On the backend I've got

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$a = $request->keyword;
echo $a;


And even that simplified code return just empty object. Can't get what's wrong. Finally I want to receive on the serverside some 'keyword' and depends on that execute some func. In jQuery and PHP I've done it like

switch ($_POST['keyword']) {
case 'A':
get_A();
break;
case 'B':
get_B();
break;
};


But I'm stuck trying to perform it in Angular + PHP... So... Thanks in advance.

Answer

try this one.

$request = json_decode($postdata,true);
$a = $request["keyword"];