How to add country list in registration form in cakephp? - 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));

Related

How should I handle forms in 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

Cakephp - keep selected value for dropdownlist after submit

How can I keep the selected value for a dropdownlist after form submission in Cakephp?
If more info (or some code) is needed just tell me please.
UPDATE
Here is part of the code in my view:
echo $this->Form->create('Chart');
echo $this->Form->input('username',
array('label'=>('Usernames List'),
'default'=>('Select username'),
'options'=>$usernames, 'selected'=>false));
echo $this->Form->end('Create Chart');
So, when I press 'Create Chart', the dropdownlist doesn't keep the username that I selected, but it goes back to the first one.
The Form helper uses the data stored in $this->data to prepopulate fields. Make sure that when you are submitting the form, the view that is rendered after has the appropriate model/key data stored in $this->data in order for the Form helper to correctly fill in the appropriate values.
Can we see your controller action possibly? That may help draw a more accurate conclusion.
you should never use the view to set defaults or values (especially selected/value is wrong as it - like your code - destroys the idea of persistent forms).
use the controller instead
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ (Default Values)
add value in dropdown like this:
<?php echo $this->form->select('Schedule.showsid', array('0'=>'title', '1'=>'description'));?>

How to generate 'a href="javascript:void(0)"' like link in cakephp?

How to generate 'a href="javascript:void(0)"' like link in CakePHP?
I make an application, the content will insert into the editor textarea when user click a list of image. I add a class to these images and write some code in the javascript file. Everything is going well.
But the link of the image is a URL address, but not 'href="javascript:void(0)' like URL. Anyone could tell me how to make it in CakePHP?
Thanks in advance!
<?php
echo $this->Html->link(
'/path/to/image/',
'javascript:void(0)'
);
?>
You can either set a path to the image or use the Html helper to generate the image tag code. The second parameter will set the href.
Don't believe there is any dynamic way, however when you are creating your form element you can set it in the options array 'href' => 'javascript:void(0)'

How to set form name and id using cakephp FormHelper class?

I'm trying to create a form using Cakephp's FormHelper class. The form needs to have a name and and an id. I fail to see an option for that however.
Looking at the documentation for the Formhelper, I see a lot of things, but not a way to set name and option. It's not in the source for the Formhelper either. How are these values set?
Cakephp v1.2 is the version of cake i'm running here
EDIT: the form is being submitted to an external destination. It is not a form associated with any model in the app.
You can simply pass any additional attributes in the $options parameter. If there's no special meaning for Cake (like url), it'll use it as HTML attributes:
echo $form->create('Model', array('id' => 'myId'));
<form id="myId" method="post" action="/models/add">

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