How should I handle forms in CakePHP? - cakephp

I'm studying CakePHP. I read a CakePHP book, and web tutorials, but I still don't get some basic things:
I see people always create a form in View with $form->create. Can I use an HTML form like normal, or must do exactly like people do?
When a form is created in login.ctp with this code:
echo $form->create('User', array('method' => 'POST', 'action' => 'login'));
echo $form->input('email');
echo $form->input('password');
echo $form->input(array('type' => 'submit'));
echo $form->end('Login');
When I click the submit button, will the data be passed to the function login() in the Controller class?
Edited :
I tried this :
<?php
$this->Form->create("Test");
$this->Form->input("stuId",array('class'=>'inputField', 'placeholder'=>'SVxxxxxxxx'));
$this->Form->input("stuName",array('class'=>'inputField', 'name'=>'stuName'));
$this->Form->end();
?>
But it show nothing ? what is the problem :(

But it show nothing ? what is the problem :(
You have to use echo as in your first code snippet:
echo $this->Form->create("Test");
echo ...

You can use HTML for anything you want, but you'd be losing a big advantage of the CakePHP framework. The Cake HTML and form helpers help to future-proof your code. You also get the benefit of Cake's implementation of best practices in web coding. I fully recommend using those helpers.
The form data is passed to $this->request->data.

Yes, the parameters will be passed to login method.
I see $form being used in the form there, it appears you are using older version of cakephp (if $form has been instantiated with $this->Form then you are fine)
The FormHelper does lot of automagic for us and it also provides us means for added security.
I would reckon you to go with The Blog tutorial

Related

CakePHP 2:3 : How can I add fadeIn delay time in cakephp.

I have tried this effect by cakephp js helper,it's working fine.Here I have tried several ways to add fadeIn delay time,but I have field.
$this->Js->get('#sending')->effect('fadeIn');
How can I add fadeIn delay time in this effect ?
In order to create basic effects of javascript or jquery using cakephp is you may use this
JsHelper::effect($name, $options = array());
Example would be.
$this->Js->get('#sending')->effect('fadeIn', array('speed' => 'slow');
But I would suggest just code your script in plain Javascript/Jquery because it's more flexible than using the built-in JsHelper in cakephp due to limited functions and it's easy to use. Just include your script in your View.
Example using JQuery:
<?php echo $this->Html->script('your_script_name'); ?>
and in your your_script_name.js
$('#sending').delay(slow).fadeIn(1000);
Hope I have helped you man.
Cf docs: http://book.cakephp.org/2.0/fr/core-libraries/helpers/js.html#JsHelper::effect
You have an array "$options" with a "speed" key

where to load javascript in a view page Cakephp

i am working on a Cakephp 2.x .. right now i am loading my js and css files like this
View/Layout/default.ctp
<?php echo $this->fetch('css'); ?>
<?php echo $this->fetch('script'); ?>
<?php echo $this->fetch('scriptBottom');?>
and in each view file i am doing this because there are some pages i am loading different css and js files
for example
View/users/index.ctp
echo $this->Html->script('libs/modernizr.custom',array('inline' => false));
$this->Html->css('reset3860.css', null, array('inline' => false));
echo $this->Html->script('libs/jquery-1.8.2.min', array('block' => 'scriptBottom'));
now the first problem i am facing right now is when i have to write js at the bottom of view page for example if i am submitting a form through ajax .. i can't write at the bottom of the index page ... because if i write there the js will go at the middle or center of the page so now what i do is i go the default.ctp and write there..
so i want to write at the bottom of the index page so it can easily managable and i can see it well what i am doing
the second question is what is the best way to manage all this ... or is there a way that i can make one file of cs and js ... in which there is only the css and js files are loaded ..and then i include that file in my default . ctp page
You should place scriptBottom block at the bottom of your layout, after content block:
<body>
<?php
echo $this->fetch('script');
echo $this->fetch('content');
echo $this->fetch('scriptBottom');
?>
</body>
And then in your views (notice - NO echo):
$this->Html->script(
'libs/jquery-1.8.2.min',
array('block' => 'scriptBottom')
);
$this->Html->scriptBlock(
"alert('Boom!');",
array('block' => 'scriptBottom')
);
here is very good concept and also i have applied in one of my application, it is working very well
it compatible with CakePHP 2.1, and packaged it as a plugin. And also upgraded the CSS compression from CSSTidy to the more recent and better maintained CSS Min.
The plugin is quick and easy to install. The installation instructions are somewhat long - but that's just to provide clarity.
for detail view you can refer Detail link Cakephp.org also you can see the plugin document and how to use it in your appliation

How to add country list in registration form in cakephp?

I have simple registration form. I just want to add dropdown country list in registration form in cakephp. Please give me simple and detail description of what to do in all related files (like changes in module, controller and .ctp files). I have country list in my database table 'countries'.
In register.ctp i did this:
echo $form->input('country_id');
I am very new in cakephp, please help me.
Thanks!
Use Find to get a List of all of your countries...
$this->set('countries', $this->Country->find('list',array('Country.id','Country.name')));
Then in your view use the form helper and pass your countries to it via the options parameter.
echo $this->Form->input('country_id', array('options'=>$countries));
Use following syntax Just replace User model with appropriate model that you are using.
$this->set('countries',$this->User->Country->find('list'));
In controller (make sure the column names are correct):
$this->loadModel('Country');
$this->set('countries', $this->Country->find('list',array('Country.id','Country.name')));
In view:
echo $form->input('country_id', array('options' => $countries));

CakePHP links in same page

I am trying to build a FAQ page, with table of contents on top and answers below it. I would like to click on a question from the table of contents and link on the same page to the corresponding answer. How can I do this in CakePHP, by using $this->Html->link() method?
Thank you!
use something like this for the link:
$this->Html->link($question_title, $this->here . '#question-' . $question_id);
and then for later down the page put the answers in something like
<div id="question-<?php echo $question_id; ?>"><?php echo $answer_text; ?></div>
obviously the vars will be something like $question['Question']['title'] in cake and the Html->link url could be done with an array like
$this->Html->link($question_title, array('action' => 'faq', '#' => 'question-' . $question_id));
just as long as the url part before the # exactly matches the current url.

CakePHP passing parameters to action

Hi im kinda new in cakephp and having a lot of trouble adjusting.. Here's my biggest problem ..
Im trying to pass a parameter to an action, it does load, but when my script goes from the controller to the view, and goes back to the controller again, its gone.
CONTROLLER CODE
function add($mac = 0)
{
if(isset($this->params['form']['medico']))
{
$temp= $this->Person->find('first', array('conditions' => array('smartphones_MAC' => $mac)));
$id= $temp['Person']['id'];
$this->Union->set('events_id', $id+1);
$this->Union->set('people_id', $id);
$this->Union->save();
}
VIEW CODE (This is a menu, i only have one button right now)
<fieldset>
<legend>SELECCIONE SU ALERTA</legend>
<?php
echo $form->create('Event');
echo $form->submit('EMERGENCIA MEDICA',array('name'=>'medico'));
echo $form->end();
?>
</fieldset>
When you create the form you don't include the additional url parameters or the fields as inputs. Without either of these the parameters will vanish as they are not part of the new request. You can append additional parameters to the form submission url with
$form->create('Event', array(
'url' => array('something', 'somethingelse')
));
This will create a form that points at /events/add/something/somethingelse.
I'm no big fan of using some helpers (like $html) or some methods (like $form's create() and end()). I kinda didn't get your problem, but I think it might be that you have to make a POST request to the same url you are actually into.
<form method="GET" action="<?=$this->here ?>">
Maybe you should give a further explanation of what you are trying to achieve.
You might want to try using named parameters.
I asked a similar question which you might find helpful:
cakephp adding record with some parameters fixed

Resources