19

I want to fetch posted data. But I am using no form. The data is postet by a jquery script with method post and I would like to fetch it.

I know how to fetch parameters

$id = $this->getRequest ()->getParam ( 'id', null ); 

and form values

$message = $form->getValue ( 'message' ); 

however I want to access post data and not parameters or form values. Any ideas?

4 Answers 4

59

Here is my solution;)

$this->getRequest()->getPost('id', null); 
Sign up to request clarification or add additional context in comments.

4 Comments

NULL is the default value, no need to specify it.
Typical ZF2 - found what I was looking for here but not in the ZF2 documentation.. Thanks
if nothing is passed to getPost, then it will return all the data. It will act like $_POST. Just used it and it worked fine. Thanks for sharing.
How to have Lazy mode on? like $post= $this->getRequest(); and then anywhere access it like $post->username where username is the $_POST['username'] ?
18

Actually, this might be more of what you're looking for.

$this->getRequest()->getRawBody(); 

https://framework.zend.com/manual/1.12/en/zend.controller.request.html

2 Comments

Is there a way to get them as array(key value pairs).
@AbdulBasit please expand; I'm not sure I understand your question.
7

Here is an other example:

$this->getRequest()->getPost()->toArray() 

1 Comment

$this->getRequest()->getPost() seems to already be an array, and thus calling toArray() on it results in an error. This is what I needed though, +1
2

Try this:

$request = $this->getRequest(); $request->getPost('field_name'); 

1 Comment

While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.