I submit my form by ajax, here what I gets in controller:
$request->getContent()
return
string 'comment[header]=vcvdfgdfg&comment[body]=dfgfdgdf&comment[_token]=nV0QYu82KWFb-wRIlIoY4MKM6-WUfeFoMidjBHfpupA' (length=120)
when I try
json_decode($request->getContent(), true) // it equal to null
What I am doing wron?
That's not a json string. If you want to parse that string and get an array you have to use the parse_str function, and you need to set the second parameter to have the data put in an array instead of into individual variables.
$get_string = "pg_id=2&parent_id=2&document&video";
parse_str($get_string, $get_array);
print_r($get_array);
Or, if you're using Symfony2 you can access them this way:
// $_GET parameters
$request->query->get('name');
// $_POST parameters
$request->request->get('name');
Related
I have the following angularjs code for posting value to php:
$scope.aToUn = function(aName) {
//$scope.question.question_16 =UnionsId;
var outputType=[{type:'aName',action:'none',PassId:aName}];
$http.post('addressManagement.php',outputType).success(function(dataJsonUnions) {
//alert(dataJsonUnions);
$scope.question =dataJsonUnions;
//question or dataJsonUnions is an object that passed from json,
//something like question={{id="2",questionvalue="Name"}}
//calling another function
$scope.anotherFunction(questionvalue); // here i want to pass only value that from json eg. 'Name' only)
});
};
I am getting json object value, which i want to do something with that only single value. (object contain only one single value, dont want if/else or foreach)
Did you try
$scope.anotherFunction($scope.question[0].questionvalue);
As i understand your question, You first need to conver json response to object by using angular.fromJson() and then pass object's property for example:
$scope.question = angular.fromJson(dataJsonUnions);
then call function with argument that you want to pass:
$scope.anotherFunction($scope.question.questionvalue);
hope this helps you.
If the response is a JSON object and if you want to access the value of field 'questionValue' then you can try the below.
var json = JSON.parse(dataJsonUnions);
json.questionValue; // THis will fetch you the value
//OR
json['questionValue'];
// Then you can pass this value to any function that you need
I am trying to pass the value to a function to get the following url:
search?channelIds=1,2,3&streamName=wew&page=0&size=10
code inside function:
$this->request->set("url","broadcasts/pubic/search");
$this->request->set_fields(array(
'channelIds'=>$channel_ids,
'streamName'=>$stream_name,
'page'=>$page,
'size'=>$size
));
$result = $this->request->send();
Here i pass the values in this format.How will give pass value for channelIds=1,2,3? i use http_build_query for url formation. $channel_ids=array("channelIds"=>1, "channelIds"=>2, "channelIds"=>3); $stream_name="wew"; $page=0; $size=10; $getlivebroadcast = $api-searchBroadcast($channel_ids,$stream_name,$page,$size);
How will i set the value?
You can use the urlencode function to encode the channel ids. Your function may look like:
$this->request->set("url","broadcasts/pubic/search");
$this->request->set_fields(array(
'channelIds'=>urlencode($channel_ids),
'streamName'=>$stream_name,
'page'=>$page,
'size'=>$size
));
$result = $this->request->send();
My php file receives a post from ajax call. The string received by the php file is as follows :
array(1) { ["userid"]=> string(21) "assssssss,camo,castor" }
I am trying unsuccessfully to decode this string then loop through the values in the array. I have tried the following :
$myarray =json_decode($_POST["userid"],true);
foreach ($myarray as $value) {
//do something with value
}
I am not sure whether the decode is the issue or my syntax to loop through the PHP array.
The POST data you'd want to manipulate is stored in $_POST['userid]
In case you're trying to access this comma separated user ids, you need to convert this to an array first using explode(). And then loop through these id's.
if (isset($_POST)) {
$user_ids = $_POST['userid']; // assssssss,camo,castor
$user_id_arr = explode(',', $user_ids); // Converts string to array Array (0 => assssssss, 1 => camo, 2 => castor)
foreach ($user_id_arr as $user_id) {
//Statements
}
}
$_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. So when you decode with json_decode that will decode JSON string to Object/Array.
But in your scenario you have not passed JSON String to $_POST so it not looks decoding.
The string you have passed into your $_POST is not JSON, so json_decode will not work on some random comma seperated values.
You can either pass in real JSON, or just use the explode method of splitting these values:
// explode example
$users = "assssssss,camo,castor";
$usersarray = explode(",", $users);
Encoded URL
www.example.com/apps/?bmFtZTE9QUhTRU4mbmFtZTI9TUFFREEmcGVyY2VudD02NQ
Decoded URL
www.example.com/apps/?name1=AHSEN&name2=MAEDA&percent=65
now i want to get params from encoded URL
ob_start();
$name1 = $_GET['name1'];
You probably have the decoded url somewhere in a variable. If you extract the query part from it (the part after the ?), you can feed that query string to the function parse_str.
If you use parse_str with just the query string as argument, it sets the appropriate variables automatically. For example
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// feed it into parse_str
parse_str($my_query_string);
would set the global variables
$name1
$name2
$percent
But I'd advise to make use of the second parameter that parse_str offers, because it provides you with better control. For that, provide parse_str with an array as second parameter. Then the function will set the variables from your query string as entries in that array, analogous to what you know from $_GET.
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// provide parse_str with the query string *and* an array you want the results in
parse_str($my_query_string, $query_vars);
echo $query_vars['name1']; // should print out "AHSEN"
UPDATE: To split your complete decoded url, you can use parse_url.
You can use the function urldecode.
This should get you started...
<?php
$query = "my=apples&are=green+and+red";
foreach (explode('&', $query) as $chunk) {
$param = explode("=", $chunk);
if ($param) {
printf("Value for parameter \"%s\" is \"%s\"<br/>\n",
urldecode($param[0]), urldecode($param[1]));
}
}
?>
I have a key/value pair passed from Javascript via $.post as data : user_id.
I've brought it into PHP with $data = $_POST['data'] and when I vardump() that I get {"id":"1"}" as expected. However, I'd like to just access the value of 1.
How would I do that?
It's just JSON. Use json_decode() to turn it into an object (or an array if you so choose) and then get the value of ID using standard object member variable access methods:
$data = json_decode($_POST['data']);
echo $data->id;
Demo
If you're using PHP 5.4+ (using array syntax and array dereferencing):
echo json_decode('{"id":"1"}', true)['id'];
Demo
You can also try this:
$data = json_decode($_POST['data'], true)['id']