I'm new with programmation JQuery, Json into PHP.
my doubt is this:
I have a chain Json as the following example
[
{"idwork":"1","status":"to_do"},
{"idwork":"2","status":"in_process"},
{"idwork":"3","status":"testing"}
]
and this chain is lodged in a variable called "data". I want post this to a class, named operation where it receive this values idwork and status, to do a consultation into SQL.
Class operation
<?php
require_once('../../clases/tarea.php');
$operacion=$_REQUEST['operaciones'];
$tarea_temporal=new tarea($_REQUEST['idtarea'],'','',0,0,0,0,0,$_REQUEST['status']);
switch($operacion)
{
case 'Modificar':
$tarea_temporal->update_status();
break;
}
header('Location:../baraja/task.php.php');
?>
I want to know how to send this variable in a form
You can use json_decode, and then u can access the data like object or array... At doc u have an example:
http://es1.php.net/json_decode
Related
I am trying to retrieve some part of request() in my Form Request class named StoreApplicantLanguage.php. The request key called 'languages' and it has an array of objects containing a key-value pair to be stored in my `applicant_languages' table.
Here is my JSON request from Postman:
{
"languages": [
{
"language": "English",
"capability": 1
}
]
}
Looks normal right?! But, when I'm trying to get the values of the languages key like this:
$requestLanguages = request()->languages;
dd($requestLanguages);
, it shows null.
I tried to restart my server, do php artisan config:cache, but none are works. But when I change the key name in the request object to language, it works!
Also, the request object has another named field like families, and I can get the values inside by doing request()->families.
I have no idea at all how this can be happen. Anyone can explain my case, please!
Thanks in advance!
Edit: From Malkhazi Dartsmelidze's answer I realized that I misstyped the question. I didn't write comma after '1' value in my JSON request
It works fine on my system.
Maybe you that's because you are passing invalid json.
{
"languages": [
{
"language": "English",
"capability": 1
}
]
}
Try passing this JSON (I deleted last comma after '1')
Also note that Request is object and there is properties that are used already and $request variable can return it. You can use $request->get('languages') to get parameter from request
I'm working with laravel 5 framework, i want to create a rest API and i'm getting some problems.
I have a method post that receives Json and i can't access that content, always say is [] or null
That's my controller function:
public function Register()
{
$teste = Request::json()->all();
$name=$teste['wtf'];
return $name;
}
That returns me this error "Undefined index: wtf" because the array $teste is null
How can I receive JSON properly?
From the image you showed in the comment, I realize that the data you are sending is as form-data and not JSON. Please select raw option and choose json instead of text from drop down and retry what #Moppo said
I'm following Album tutorial on Zend Framework 2's website, and I want to send object that have been converted to Json and pass controller via AJAX, I can invoke the method on the controller as well as receive Json from controller, but I don't know how to send parameter to it.
Thanks for reading and sorry for my bad English.
Do you mean pushing parameters to your controller via AJAX, like this?
$.post('/your/url', { value: val, row: row, element: element }, //parameters
function (ret) {
});
I am trying to create an api with Recess and I have a question about its JsonView. Currently, if I do a GET request on, for example, /users/1 (which routes to a function that gets all the details for the user with id 1 and responds with Json), I get the following:
{"users":{"id":"1","username":null,"password":null,"datejoined":false}}
How can I make it so that I get the following instead:
{"id":"1","username":null,"password":null,"datejoined":false}
That is, I don't want all the details wrapped inside "users":{}.
By default, Recess's JsonView responds with the properties of your controller. So your $users property is getting directly encoded into JSON.
You can override this by returning a custom response object:
return new OkResponse($this->request, (array)$this->users);
Not sure about recess specifically, but if you are using JsonView method/function with an input parameter (array) $result, then changing $result to $result['users'] may give you the answer you are looking for.
For example using plain PHP:
first object: echo json_encode($result);
second object: echo json_encode($result['users']);
I have created a table in DB with name "member" having field "name", "college", "email", "zid" now I have created a class(in php) like
class member
{
private $name,$college,$email,$zid;
private function adduser
{
//function definition
}
public function autherise($id)
{
//function definition
}
}
now at index page I am taking these value as input from user using html form(validated by JS) now at action page of form I want to create object as obj=new member(); then calling the class function as obj->autherise($_post['zid']);
I want the defintion of autherise and adduser function like autherise check the uniqueness of zid and the calls adduser with remaining post variables and store them to object then add whole object in one query to DB.
I dont wan
insert into member(name,email,college,zid) values('$name,$email,$college,$zid')
I want to enter obj directly to the db
You can modify anything in functions
Thanks in Advance!!!
An "easy" solution to store a whole object somewhere, like in a database, is to serialize it – i.e. transform it to a string ; and, then, store that string in a single field in the database.
This can be done, in PHP, using the serialize function – and to de-serialize the string to an object, you'll use the unserialize function.
Another solution would be to serialize your object to the JSON format – nice thing with that is that JSON can be used directly from Javascript, and from lots of different programming languages, using the right libraries.
For JSON, see json_encode and json_decode
As a sidenote : note that if you store your data as serialized strings, it will be much harder to manipulate them in SQL : you will not be able to update them using a simple SQL query, for instance; and searching will be hard too.
This means you'll always have to fetch your data from the database, manipulate them with PHP, and send them back to the database – which might not always be such a good idea, depending on your needs.
I'm not sure what you're asking for. But maybe...just maybe you're looking for an object relational mapper (orm) , like e.g. doctrine.
If you were looking to store an object in a database you could serialize() or json_encode() the object into a String and then store it in a field in the table.
When you are ready to use it again you can unserialize() or json_decode() the String again.