I am posting an array from angular to PHP. The array i sent is like that [92,70,86,62,75,84,95]
but in php it's turned into like this - "[92,70,86,62,75,84,95]".
My expected output from php is
{
user_id: false,
data: [92,70,86,62,75,84,95]
}
The output i am getting is
{
user_id: false,
data: "[92,70,86,62,75,84,95]"
}
The code for posting data from angular is
$scope.data = [92,70,86,62,75,84,95];
$http({
method: 'POST',
url: 'http://localhost/learn_php/api/api_set_data/',
data: $scope.data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.success(function(data) {
console.log(data);
});
The code in php is
public function api_set_data(){
$array['user_id'] = $this->session->userdata('user_id');
$array['data'] = file_get_contents("php://input");
$serializedData = serialize($array);
file_put_contents(APPPATH."assets/get_values.txt", $serializedData);
echo json_encode($array);
}
Use JSON.stringify and json_decode respectively
Javascript
data: JSON.stringify($scope.data)
PHP
$array['data'] = json_decode(file_get_contents("php://input"), true);
Related
I have added below call for ajax call
$http({
url: "http://localhost/angular-js-apis/listing.php",
method: "POST",
data: {type: "list"},
headers: {'Content-Type': 'application/json;charset=utf-8'}
}).then(function mySuccess(response) {
$scope.listing = response.data;
}, function myError(response) {
$scope.myWelcome = response.statusText;
});
and at server site at below file code
http://localhost/angular-js-apis/listing.php
<?php
include("database.php");
echo "<pre>";var_dump($_POST);die("test to die");
?>
but in my server it prints only empty value of $_POST.
can anyone tell me what is an issue over here?
Try the following snippet
Angular
$http.post(
url: "http://localhost/angular-js-apis/listing.php",
JSON.stringify({type: "list"})
).then(function (response) {
$scope.listing = response.data;
}, function (response) {
$scope.myWelcome = response.statusText;
});
Server
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
I am starting with angularjs with ngStorage. I can save and display data successfully.
I display value as before
{{myobj.session}}
I would like to pass whatever stored value into php variable. Shown below is my imaginary logic and I know thats not gonna work. My question is how to assign such value into PHP variable in a correct manner?
<?php
$name = {{myobj.session}}
?>
You can use this angular variable: students.tiffen_type
PHP variable : $value
<?php
$value = "{{ students.tiffen_type }}";
?>
You can do a ajax request like this:
$http({
url: "urltopost.php",
method: "POST",
data: {
data: variable
}
}).success(function(response) {
console.log(response);
});
And on the backend you can get the variable like this
<?php
$request = json_decode( file_get_contents('php://input') );
$variable = $request->data
From there you can do everything you want with that variable, still I'm not sure what are you trying to achieve.
You can't assign directly angularjs value to php variable, use the following method it will help you
$http({
method: "POST",
url: "test.php",
data: {
data: postvariable
}
}).success(function(response) {
console.log(response); //get the echo value from php page
}).error(function(response) {
console.log(response);
});
test.php
<?php
$data = json_decode(file_get_contents("php://input"));
echo $data->data;
?>
send.js
$http({
method: "post",
url: "ajax/request.php",
data: {
angular_var: $scope.angular_var // $scope.angular_var is angular variable e.g. ng-model="angular_var"
}, headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
console.log(data)
});
ajax/request.php
$request_arr = json_decode( file_get_contents('php://input') );
$angular_var = $request_arr->angular_var;
I have a problem that puzzles me. I have an ajax function that sends a json object, and I see the JSON parsed in the F12 Chrome Headers, and I receive the success alert.
$(document).ready(function() {
var test = {'bob':'foo','paul':'dog'};
$.ajax({
url: "test.php",
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(test),
success: function(data) {
alert("Bien: " + data);
},
failure: function(errMsg) {
alert("Mal: " + errMsg);
}
});
});
But in my PHP page I cannot see any POST, anything. I can see that my post is received but anything else:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "post"; //Result 'post'
}
foreach( $_POST as $stuff ) {
echo $stuff; //Nothing at all
}
print_r(json_decode($_POST["data"], true)); // Undefined index: data
In the same code I use
$.post( "test.php", { data: { name: "John", time: "2pm" } } );
and works, then is something related with the code, but I cannot really see waht is it.
Thank you for your help!
try this instead
$results = json_decode(file_get_contents('php://input'));
echo $results->bob //Result foo
I am trying to consume a JSON array in PHP sent via JQuery. The data being sent to the server is in this format:
[{"id":"7","start":"00:00","end":"02:30","date":"2013-11-15"},{"id":"10","start":"23:00","end":"23:30","date":"2013-11-15"},{"id":"5","start":"13:00","end":"14:00","date":"2013-11-16"},{"id":"6","start":"18:00","end":"18:45","date":"2013-11-16"}]
I am sending the above data to the server through this function:
$('#updateOverallChanges').click(function(event){
var formArray = new Array();
$('.updateForm').each(function( index ) {
formArray.push($(this).JSONFromSerialize());
});
$.ajax({
url: 'inner/formTester.php',
data: JSON.stringify(formArray),
type: 'POST',
contentType: "application/json; charset=utf-8",
});
});
The function JSONFromSerialize is responsible for converting the form in meaningful json data:
(function($) {
$.fn.JSONFromSerialize = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
I am using the following PHP code to process the JSON array:
<?php
$params = json_decode($_POST[]);
echo $params;
?>
But the above code give the following output:
Fatal error: Cannot use [] for reading in C:\xampp\htdocs\holidaymanager\inner\formTester.php on line 2
Second attempt
Modified the payload to be a name value pair:
var postData = {
data : formArray
};
$.ajax({
url: 'inner/formTester.php',
data: JSON.stringify(postData),
type: 'POST',
contentType: "application/json; charset=utf-8",
});
The post data is:
{"data":[{"id":"7","start":"00:00","end":"02:30","date":"2013-11-15"},{"id":"10","start":"23:00","end":"23:30","date":"2013-11-15"},{"id":"5","start":"13:00","end":"14:00","date":"2013-11-16"},{"id":"6","start":"18:00","end":"18:45","date":"2013-11-16"}]}
The php code still fails:
<?php
$params = json_decode($_POST["data"]);
echo $params;
?>
Error:
Notice: Undefined index: data in C:\xampp\htdocs\holidaymanager\inner\formTester.php on line 2
Please advice how can I consume a JSON array sent over to the backend written in PHP.
$.ajax({
data : {data:JSON.stringify(postData)},
})
in php
$params = $_POST['data']; // add this line
$params = json_decode($params,true);
print_r($params);
or else
$params = json_decode($_POST,true);
print_r($params);
you have pass 2nd argument true with json_Decode()...
hope it may help you
I try to make a post query to save my array in database. server side is PHP. My angular part:
$http({
method: 'POST',
url: "addOrder.php",
data: myJsonedArray,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
Angular make a post query to addData.php, but in php file my command
print_r($_POST); or print_r($_REQUEST);
give me empty Array();
How to fix it? Thanks
UPDATE:
if I try this example in jquery - I have he same result - empty array, but if I try with "test_var" - example works well:
$.post("addOrder.php", { "test_var": da }, function (data) {
console.log(data);
});
How to make the same result? I've tried
$http({
method: 'POST',
url: "addOrder.php",
data: { "test_var": da },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
but no result (
Perhaps this helps: http://www.cleverweb.nl/javascript/a-simple-search-with-angularjs-and-php/
$data = file_get_contents("php://input");
$objData = json_decode($data);
Also, I find $resource much easier to use...