well i have this problem about sending multi data from JS to PHP, i'm using angularJS. the problem is i can't receive any data, i'm a beginner in this stuff like AngularJS.this my my JS part:
var data = {"name":name, "cin":cin, "job":job};
var config = {
params: data,
headers: { 'Content-Type': 'application/json' }
};
$request = "php/readFromDatabase.php";
$http.post($request, config).then(function (response) {
$scope.collectionData = response.data.records;
});
this should be fine and work i have my data as struct (object) the name and cin and job are variables when i click button using angularJS controller this function should be launched and call an PHP file :
<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
$name = $_POST['name'];
i use those information to look for data in database and then i echo them as JSON format for that i want to use $http.get{} so i can call the response.data.records and not to use the $ajax.{}. the problem is this $_POST['name'] doesn't work; even this didn't work:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$name = $request->name;
As i said, i'm new here so plz tell me what i'm doing wrong because i didn't find anything that can help me, i tied everything i have found in internet. hope if someone can help me and thank you in advance.
Edit :
this is my JS file:
app.controller('shearch_data', function ($scope, $http) {
$scope.ShearchFunction = function () {
var name = " ";
if ($('#toggle_name').prop('checked') == true)
name = $('#inputName').val();
var cin = " ";
if ($('#toggle_cin').prop('checked') == true)
cin = $('#inputCIN').val();
var job = " ";
if ($('#toggle_job').prop('checked') == true)
job = $('#inputJob').val();
var data = {'name': "ffss", 'cin': cin, 'job': job};
var url = "php/readFromDatabase.php";
$http.post(url, data).then(function (response) {
$scope.collectionData = response.data.records;
}).catch(function (err) {
alert('something went wrong')
});
};
});
my PHP :
<?php
header("Access-Control-Allow-Origin: *");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$name = $request->name;
$cin = $request->cin;
$job = $request->job;
$output = "";
$output .='{"Name":"'.$name.'",';
$output .='"Job":"'.$job.'",';
$output .='"Cin":"'.$cin.'"}';
$output = '{"records":[' . $output . ']}';
echo($output);
this is what i'm trying to do for test, send data from JS to PHP using http.post then i receive it as json, it's jsut for test for now.