Passing Data from Dropbox Chooser to controller/model in Cakephp - cakephp

EDIT:
I am getting the JSON results from the dropbox chooser, but they are not being parsed properly to be put into the database. I can't figure out what I am doing wrong, if I should be doing a JSON decoder in my controller or if it is something else.
Controller Code:
<?php class DropboxfilesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file is now available :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="Dropboxfilechooser" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}?>

The actual problem is looking at the source code, the form action is not going to the proper controller, it is just going to the cake/homes. Just change that and the problem is solved.

Related

passing data to controller from view in cakephp

I'm new to cakephp. I went through Andrew Perkins's video tutorials. It is a great tutorial for absolute beginners.
What I want to do is get the username to a hidden field from login page and keep it in a view.and pass it to the controller and save it to the database when it is necessary.
right now I have a add.ctp, BabiesController.php as the controller and baby.php as the model.
since I haven't completed the login page I would like to hard code the username for instance.
here are my codes. hope you guys can help me.
this is the database.emails address has been used as primary key for babyprents table.parent column int the babies table is a foreign key to the email address in the babyparents table.
Model : baby.php
<?php
class Baby extends AppModel {
var $name = 'Baby';
}
Controller : BabiesController.php
<?php
class BabiesController extends AppController {
var $name = 'Baby';
function index()
{
$this->set('babies',$this->Baby->find('all'));
}
function add()
{
if(!empty($this->data))
{
$this->Session->setFlash($this->data);
if($this->Baby->save($this->data))
{
$this->Session->setFlash('Successful');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('unsuccessful');
}
}
}
}
View: add.ctp
<h2>Sign up</h2>
<?php
echo $this->form->create('Baby',array('action'=>'add'));
echo $this->form->input('firstName');
echo $this->form->input('middleName');
echo $this->form->input('lastName');
echo $this->form->input('birthday');
echo $this->form->input('age');
echo $this->form->input('doctor');
echo $this->form->end('Sign up');
?>
all the data has been saved except for email address.any guide will be appreciated. thank you all.
In your controller use saveAll($this->data):
function add()
{
if(!empty($this->data))
{
$this->Session->setFlash($this->data);
if($this->Baby->saveAll($this->data))
{
$this->Session->setFlash('Successful');
$this->redirect(array('action'=>'index'));
}
else{
$this->Session->setFlash('unsuccessful');
}
}
}
In your add.ctp put this line
echo $this->form->input('Babyparent.email');

Passing data via POST from an element to controller/model Cakephp

I am pretty new to cakephp, I have an uploader form and would like to pass all of the data using POST to the controller and model so that it will save to the database. So far there are no cakephp errors occuring but no data is being passed.
Controller Code:
<?php class Controller extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->File->create();
if ($this->File->save($this->request->data)) {
$this->Session->setFlash(__('Your file has been liberated :)'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('Something went wrong!'));
}
}
}
}?>
View Code:
<?php echo $this->Form->create('File'); ?>
<input type="uploader" name="selected-file" style="visibility: hidden;"/>
<?php echo $this->Form->end('Finish'); ?>
Model Code:
<?php class File extends AppModel {}
?>
Please, check input specification you can see there, that there are no "uploader" value for type attribute. Maybe you mean type="file".
Also, if you want to upload files, please, use
echo $this->Form->create('File', array('type' => 'file'));
this will produce correct enctype attribute enctype="multipart/form-data". such enctype needed if you send files by POST.
Also, you use style: style="visibility: hidden;" which should hide file input field, so, maybe you use some flash uploader or ajax, but this is not demonstrated in your code. If you provide more code, we can provide more concrete help to you.
and counter question: how you debug your code?

Json does not popup as download file in cakephp 2.3

I am using cakephp 2.3 and want to generate list of users in json format.
controller name: users
method: list
Earlier, I have done it in cake 1.3.x and when user tries access method via url in browser then output comes as download file but when i am doing it in cakephp 2.3 then it is showing me json output on browser page itself instead of download file.
here is my code:
Controller:
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $helpers = array('Form', 'Html', 'Js', 'Time');
public $components = array('RequestHandler');
var $layout = 'js/default';
public function list() {
$this->log("i got data in user add:");
$this->log($this->request->data);
$posts['id']['name']='kapil';
$this->set(compact('posts'));
}
}
list.ctp file
<?php echo json_encode(compact('posts')); ?>
view/js/default.ctp
<?php echo $scripts_for_layout; ?>
<?php echo $this->fetch('content'); ?>
when i try to access my function in browser then i get this output
url: http://localhost/project/users/list
{"posts":{"id":{"name":"aditya"}}}
This output is correct but should come in download file format. I don't know what is wrong here.
If you are trying to force download, your code must make use of Response object.
Here is an example taken from CsvExport Behavior:
public function export() {
$this->autoRender = false;
$modelClass = $this->modelClass;
$this->response->type('Content-Type: text/csv');
$this->response->download( strtolower( Inflector::pluralize( $modelClass ) ) . '.csv' );
$this->response->body( $this->$modelClass->exportCSV() );
}
So your code would look like this :
public function list() {
$this->autoRender = false;
$this->log("i got data in user add:");
$this->log($this->request->data);
$posts['id']['name']='kapil';
$this->set(compact('posts'));
$modelClass = $this->modelClass;
$this->response->type('Content-Type: application/json');
$this->response->download('list.json');
}
Hope this helps!

Sending string/text to a method/function in cakePHP

Good Day to all. I am currently developing a chat application using cakePHP. It will be a chat application that focuses on answering questions. That means the user will receive an automated response based on his/her question. I am working on the chat interface right now that doesn't require the user to login. The chat application will only be interacting to a database table once the user has sent the question. Now my problem is on how to send the question to a method in the controller where it will be parsed. I tried to do the following in the view file:
<!--View/People/index.ctp-->
<h1>This is the chat interface</h1>
<?php $this->Html->charset(); ?>
<p>
<!--This is the text area where the response will be shown-->
<?php
echo $this->Form->create(null);
echo $this->Form->textarea('responseArea', array('readonly' => true, 'placeholder' =>
'***********************************************************************************
WELCOME! I am SANTI. I will be the one to answer your questions regarding the enrollment process
and other information related to it. ***********************************************************************************', 'class' => 'appRespArea'));
echo $this->Form->end();
?>
</p>
<p>
<!--This is the text area where the user will type his/her question-->
<?php
echo $this->Form->create(null, array('type' => 'get', 'controller' => 'people', 'action' => 'send', ));
echo $this->Form->textarea('userArea', array('placeholder' => 'Please type your question here', 'class' => 'userTextArea'));
echo $this->Form->end('Send');
?>
</p>
This is the controller:
<!--Controller/PeopleController.php-->
<?php
class PeopleController extends AppController{
public $helpers = array('Form');
public function index(){
}
public function send(){
//parsing logic goes here
}
}
?>
As you can see, I am telling the form in index.ctp to point the action to the send() method in PeopleController so it can parse the question before interacting with the database. The problem that arises when I click the button is that I am always redirected to /users/login which is not what I want to happen. I just want the application to point itself to /people/send. What seems to be the problem in that case? I have tried to look for answers both in the Internet and in the documentation and then tested them but nothing has resolved the problem so far. Can anyone please help me on this? I've been trying to resolve this for so many days.
I keep on getting this error:
Missing Method in UsersController
Error: The action *login* is not defined in controller *UsersController*
Error: Create *UsersController::login()* in file: app\Controller\UsersController.php.
<?php
class UsersController extends AppController {
public function login() {
}
}
If you are using Auth Component, then you might need to change your PeopleController code:
<!--Controller/PeopleController.php-->
<?php
class PeopleController extends AppController{
public $helpers = array('Form');
public beforeFilter()
{
parent:: beforeFilter();
$this->Auth->allow('index', 'send');
}
public function index(){
}
public function send(){
//parsing logic goes here
}
}
?>
This is because of you used people/send as a form action. And the user is not logged in, it means there is no any Auth session has been set. Thats why it always redirect the user to login page, and if there is not login page, then it will show you the error.
So I made the send() method also public, so that anyone can access it.
Hope this concept will help you.

Validation doesn't working in cakephp2.x

I am new in cakephp. I wrote a validation ctpfile in cakephp 2.6.7 for viewing login and logout word but the validation doesn't work.
My code is:-
<?php
if (!$authUser) {
echo $this->element('logout-header');
} else {
echo $this->element('login-header');
}
?>
How can I write validation in ctp file for viewing login and logout word in my page header?
Why you wrote a validation ctp? put your validation rules in model
http://book.cakephp.org/2.0/en/models/data-validation.html
In your AppController's beforeRender() callback set the authUser view variable by retrieving the logged in user:-
public function beforeRender() {
parent::beforeRender();
$this->set('authUser', $this->Auth->user());
}
Then the view code in your question should work as expected.
I have a solution, which I regularly use when I worked in cakephp.
in AppController.php
class AppController extends Controller{
public function beforeFilter() {
parent::beforeFilter();
$userInfo = array();
if($this->Auth->user('_id')){
$userInfo['User'] = $this->Auth->user();
Configure::write($userInfo);
}
}
}
And after this in view .ctp file
<?php
$authUser = Configure::read('User');
if (!$authUser) {
echo $this->element('logout-header');
} else {
echo $this->element('login-header');
}
?>

Resources