In Codeigniter I do this
$p=$this->input->post();
to get all objects posted but I don't know if there is something similar in cakephp to get all posted variables from a form ? I am writing a function to get posted password and save it into database in place of the old password recorded there.
I use native php to get 'posted' variables from a form, (I am not familiar with cakephp form usage) that is why, so instead of using $_POST['sssss'] what should I do now ?
Thank you for any help.
$value = $this->request->data('key');
Please for further reference, read the manual. It's so much easier and better for yourself to figure it out by yourself.
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-post-data
for the GET method
$this->request->query['category-name'];
and POST method
$this->request->data
http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-querystring-parameters
You can check if posted a form by using
if (!empty($this->data)) {
print_r($this->data);
}
The Post data must be in data to show up in $this->request->data.
Example:
// input field
<input type="text" name="data[foo]" value="bar" />
// in your controller
debug($this->request->data);
To check if posted a form, please use:
if ($this->request->is('post')) {
pr($this->request->data);
}
If you want to get a specific field of the table can move so:
if($this->data["Objetorastreavel"]["id"]){
}
It checks only the ID Objetorestraeval if you want to pick only one field and not post the whole page.
You should able to access form post data with:
For CakePHP 2.x
if ($this->request->is('post')) {
pr($this->request->data);
}
For CakePHP 3.4.x
if ($this->request->is('post')) {
pr($this->request->getData());
}
Documentation for CakePHP 3
You can use following to retrieve post/get data in CakePHP
For post data:
$this->request->data;
For get data:
$this->request->query;
Related
In my application , I have AdminController with actionupdate, So in YII the path becomes admin/update. Now in order to get certain users info , I use the following path admin/update?id=10 where 10 is the empID.
Is there any way to do the same thing without id part of the path, i.e. I want my path to look like admin/update? instead of (admin/update?id=10). i don't need the user want to see the id values.
Thank you!
You can send data using POST method instead of GET
With the help of javascript
Use an hidden form with post method and a input field. onclick of update button set id to input field and submit form. you will get id in controller's action without showing it in url
use in actionUpdata Yii::$app->user->id;
public function actionUpdate() {
$empID = Yii::$app->user->id;
}
i have a problem when coding my new Joomla component.
I have some input form in my custom joomla component (/components\com_custom\views\myview\tmpl\default.php )
<input type="text" class="form-control" name="jform[checkindate]" id="checkindate" value="<?php if (isset($post)) {echo $post; } else { echo 'Checkin date'; }?>">
Of courses, at the top i have this:
$jin = JFactory::getApplication()->input;
$post = $jin->get('checkindate', 'Checkin date', 'STR');
My point is to keep user post data if they faile in validation so they don't need to retype all the form, they only need to fix some error data before they can submit it again, but there is no success.
Could you please help me on this?
Thank you so much!
I'm assuming you are using Joomla jform for building forms
In jform, certain things need to take care if you want to maintain the form session.
Store the submitted form data in session - Before redirecting the user to form view after validation. you need to store the user data in user state. This is generally done in the controller file. This done by following way
$app = JFactory::getApplication();
$app->setUserState('componentname.formname.data', $data);
Please note that the first parameter is the key for your data which is stored in the user state. the second parameter is the Jinput data post by the user.
Load form data from the session if available - Till now we have saved the data in the session now we need to check whether form data available in session or not. This Is done in the load form data method written in the model.
`
protected function loadFormData()
{
$data = JFactory::getApplication()->getUserState('componentname.formname.data', array());
if (empty($data))
{
// This is the my component function to load form data
$data = $this->getData();
}
return $data;
}
3 . If the form data saved successfully then don't forgot to clear it from the session.
$app->setUserState('componentname.formname.data', null);
I have a textarea in my front-end which accepts the google map code which is an iframe. When I tried to update it, the query fails. The values is not getting inserted into the database. I use text for saving iframe in db. The code I use in model :
function save(){
$data['cmpny_address'] = $this->input->post('cmpny_address');
$data['cmpny_map'] = $this->input->post('cmpny_map');
$this->db->where('id',1);
$this->db->update('contact_us', $data);
}
I have tried sanitizing the input with htmlspecialchars, strip_tags, $this->db->escape etc. I have actually tried all the suggestions from related SO questions. But no luck. Somebody please suggest a way to fix the issue.
EDIT:
It is the <iframe></iframe> that is creating the problem. <p></p> , <h1></h1> gets through without any error.
(Posted on behalf of the OP).
It was a server issue. The support team said "modsecurity was blocking it".
No need to write any htmlspecialcharsand strip_tags Active record automatically handle all those thing.
Use set method to update your data
function save(){
$this->db->set("cmpny_address", $this->input->post('cmpny_address'));
$this->db->set("cmpny_map", $this->input->post('cmpny_map'));
$this->db->where('id',1);
$this->db->update('contact_us');
}
OR
create you data array like
$data=array('cmpny_address'=>$this->input->post('cmpny_address'),'cmpny_map'=>$this->input->post('cmpny_map'));
$this->db->where('id',1);
$this->db->update('contact_us', $data);
UPDATED
To send iframe into post you have to set
$config['global_xss_filtering'] = FALSE;
In your config.php file
I go filter and validation set up in my form, also a use $form->populate to put back $data back to my form when fail validation but it is not working.
if ($this->getRequest()->isPost()) {
if(!$searchForm->isValid($this->getRequest()->getPost()))
{
$searchForm->populate($searchForm->getUnfilteredValues());
$this->view->searchForm = $searchForm;
}
when I run this I get filteredValues in my search field instead UnfilteredValues.
What I going wrong?
thank you.
You should not apply the negation !$searchForm->isValid($searchForm->getValues(), because the isValid method actually populate the form for you,
Your code should look like:
if ($this->getRequest()->isPost()) {
if($searchForm->isValid($this->_request->getPost()))
{
// do insert, upload or others
}
}
As i said before, the isValid method actually populate form in case of invalidation of the form.
Best regards!
I know that CakePHP params easily extracts values from an URL like this one:
http://www.example.com/tester/retrieve_test/good/1/accepted/active
I need to extract values from an URL like this:
http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
I only need the value from this id:
id=1yOhjvRQBgY
I know that in normal PHP $_GET will retrieve this easally, bhut I cant get it to insert the value into my DB, i used this code:
$html->input('Listing/vt_tour', array('value'=>$_GET["id"], 'type'=>'hidden'))
Any ideas guys?
Use this way
echo $this->params['url']['id'];
it's here on cakephp manual http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params
You didn't specify the cake version you are using. please always do so. not mentioning it will get you lots of false answers because lots of things change during versions.
if you are using the latest 2.3.0 for example you can use the newly added query method:
$id = $this->request->query('id'); // clean access using getter method
in your controller.
http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query
but the old ways also work:
$id = $this->request->params->url['id']; // property access
$id = $this->request->params[url]['id']; // array access
you cannot use named since
$id = $this->request->params['named']['id'] // WRONG
would require your url to be www.example.com/tester/retrieve_test/good/id:012345.
so the answer of havelock is incorrect
then pass your id on to the form defaults - or in your case directly to the save statement after the form submitted (no need to use a hidden field here).
$this->request->data['Listing']['vt_tour'] = $id;
//save
if you really need/want to pass it on to the form, use the else block of $this->request->is(post):
if ($this->request->is(post)) {
//validate and save here
} else {
$this->request->data['Listing']['vt_tour'] = $id;
}
Alternatively you could also use the so called named parameters
$id = $this->params['named']['id'];