Cakephp two forms (sending data to different controller) - php

EDIT
First form that needs to send data to a specific controller is here
echo $this->Form->create('Message', array(
'url' => array('controller' => 'messages', 'action' => 'send')
));
echo $this->Form->input('mob_network', array(
'options' => array($list)
));
echo $this->Form->input('mob_phone', array(
'type' => 'text'));
echo $this->Form->input('message', array(
'type' => 'textarea'));
echo $this->Form->submit('Send', array('div' => false,'class' => 'btn btn-default btn-add'));
echo $this->Form->end();
controller is here
class MessagesController extends AppController {
public function send()
{
pr($this->request->data);
}
}
The problem is that i cant see this data. after clicking submit button the page acts as if it was refreshed..

You should add:
echo $this->Form->end();
to the end of both forms. Or you left that out in your code?

Related

Multiple Submit buttons in cakephp form

I have a form in a cakephp view which saves well with a single button, here is the code in the view book_form.ctp
echo $this->Form->create
(
'Book',
array
(
'url' => array
(
'controller' => 'Books',
'action' => 'save_record'
),
'class' => 'span12 the_ajaxform',
'inputDefaults' => array
(
'label' => false,
'error' => false
)
)
);
.
.
// form fields
.
.
$options =array(
'label' => __('Save'),
'class' => 'btn btn-primary',
'id'=>'saveform'
);
echo $this->Form->end($options);
.
.
This works perfect! Now i wanted to add two buttons on that form and this is what i did
$options =array(array(
'label' => __('Save & Close'),
'class' => 'btn btn-primary',
'id'=>'saveform'
),
array(
'label' => __('Save & Create New'),
'class' => 'btn btn-primary',
'id'=>'saveformnew'
)
array(
'label' => __('Cancel'),
'class' => 'btn btn-primary',
'id'=>'formcancel'
));
echo $this->Form->end($options);
But this only brings one button which wont even submit the form,where am i going wrong?
and can each button call a different method in the controller?
Thanks in advance
If you set the name of the submit button, it will have that as a key in the post data, so you can redirect using that info at the start of your action. e.g.
<?php echo $this->Form->submit('btn1value', array('name'=>'btn1'))?>
<?php echo $this->Form->submit('btn2balue', array('name'=>'btn2'))?>
clicking the first button will give post data like:
array(
[btn1] => btn1value
[YourModel] => array(...)
)
Which makes it easy to do something like:
if (isset($this->request->data['btn1'])) {
// btn1 was clicked
} else if (isset($this->request->data['btn2'])) {
// btn2 was clicked
}
I am not sure whether it is "Technically Correct", HTML4, 5 compatible or not etc. but I have always done it something like this, without any problem so far:
<?php echo $this->Form->submit('Delete it', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Undelete Selected', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Purge Selected', array('name'=>'User[formaction]')); ?>
where "User" is the model name.
Usually one form can have just one action
this lmnitation is no longer true in HTML5 where you can set the form action for every button
so: the following code works only for HTML5 browsers
echo $this->Form->button(
'Your Action Description Here',
array(
'type' => 'submit',
'formaction' => 'yourActionHere' //
)
);
Try this, This is easy to do.
<div class="submit">
<?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
<?php echo $this->Form->button('Cancel', array('type' => 'button'));?>
Try using the FormHelper's button function to create the submit button and the other buttons and just call end after that without any options. This will output the buttons and end your form for you.
See: FormHelper::button
e.g.:
echo $this->Form->button('Save & Close', array('type' => 'submit'));
echo $this->Form->button('Save & Create New', array('type' => 'button'));
echo $this->Form->button('Cancel', array('type' => 'reset'));

CakePHP: Using FormHelper to pass search query to controller parameter

In CakePHP 2.4, how do I pass a form directly to a controller parameter? My controller takes a parameter in the format /Model/index/by:keyword, however FormHelper keeps inserting a question mark and an equals sign into the URL: Model/index?by%3A=keyword.
This is the form I've been using. Is there any way to change this default behavior?
echo $this->form->create('Post', array('action' => '/index', 'type' => 'get', 'class' => 'navbar-form'));
echo $this->form->input("by:", array('label' => '', 'placeholder' => 'Search', 'class' => 'form-control'));
echo $this->form->end();
I'd try to create the form like this:
echo $this->Form->create('Post', array('type' => 'GET', 'url' => array('controller' => 'yourpostcontroller', 'action' => 'search')));
echo $this->Form->input('search', array('class' => 'form-control'));
echo $this->Form->button('Search', array('div' => false, 'class' => 'btn'));
echo $this->Form->end();
In your "Search" action (inside the controller) do not forget to read the request:
if(!empty($this->request->query['search']) ...{
//do something
}
Hope it helps.

submit button is not working in zend form

I am working on zend form and fieldsets. I have created form along with the submit button. Here is the code:
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Search Results',
'id' => 'submit',
'class' => 'submitme',
),
));
I can see all my fieldsets on view side. But, I cannot see submit button.
Here is my controller file code:
$form = $this->getServiceLocator()->get('FormElementManager')->get('Client\Form\Search\SearchForm');
return new ViewModel(array('form' => $form));
And finally this is the code for view:
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag($form);
You probably miss out the part to assign an action for your form
$form->setAttribute('method', 'post')
->setAttribute('action', $this->url())
->prepare();
Try doing something like this (as mentioned by sam) you need to set attributes
$Form = $this->Form;
$Form->setAttribute('action', $this->url('(your route name here)', array('action' => 'add', 'controller' => '(your controller name here)')));
$Form->prepare();
echo $this->form()->openTag($Form);
echo $this->formSubmit($Form->get('any other feilds that you want to see'));
echo $this->formSubmit($Form->get('submit'));
echo $this->form()->closeTag();

print values of input element after form redirection in cakephp

i am very much new to cakephp. I have created a simple form with input controls on it as follows:
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('address', array('rows' => '3'));
echo $this->Form->input('aaa', array(
'type' => 'date',
'label' => 'select',
'before' => '--before--',
'after' => '--after--',
'between' => 'Date',
'separator' => '****',
'empty' => '--select--'
));
echo $this->Form->checkbox('subjects', array('value' => 'Java'));
?>
java
<?php
echo $this->Form->input('gen', array(
'type' => 'radio',
'options' => array('m', 'f')
));
echo $this->Form->input('file', array('type' => 'file'));
echo $this->Form->input('listbox', array('options' => array(1,2,3,4,5), 'multiple' => 'multiple'));
echo $this->Form->end('Submit');
?>
i wish to print the values entered in these components on another page. how do i do that?
i tried to do it with the help of session(which seems to be inappropriate) as follows:
public function contactus() {
if ($this->request->data!=null) {
$var=$this->request->data;
$this -> Session -> write('myvar', $this->request->data);
//$this->set($var, $this->request->data);
$this->redirect(array('action' => 'contactview'));
}
}
but it outputs array and i cant use session to store each component's value.
how do i solve this?
According to cakephp book (Form: http://book.cakephp.org/1.3/view/1384/Creating-Forms)
<?php
echo $this->Form->create(null,
array('url' => array('controller' => 'recipes', 'action' => 'add')));
?>
//Output:
<form method="post" action="/recipes/add">
Therefore , just change your create function and add the url option.
Rather than using the session, I used this->data and it was able to resolve my problem.

Form post data from checkboxes lost in Firefox using cakePHP Formhelper

I'm developping my first cakePHP application and I encountered my first major problem. I have a Search/Filter form which submits data to the Index action of my controller. This is all done using cakePHP's Formhelper. I then access this data using $this->data. It works fine in chrome and IE, but in Firefox all my fields are empty. According to the post data from Firebug, the HTML is fine.
http://i.imgur.com/SznRc.png
What am I missing?
In my view :
<?php echo $this->Form->create('Contact', array('action' => 'index')); ?>
<?php echo $this->Form->input('keywords', array('class' => 'search', 'label' => false)); ?>
<?php echo $this->Form->input('sex', array('label' => false, 'multiple' => 'checkbox', 'div' => false, 'options' => array('F' => __('Female'),'M' => __('Male')))); ?>
<?php echo $this->Form->input('language', array('multiple' => 'checkbox', 'label' => false, 'div' => false, 'options' => $languages)); ?>
<?php echo $this->Form->end(array('label' => __('Search'))); ?>
In my controller :
class ContactsController extends AppController {
public $helpers = array('Html', 'Form', 'Paginator');
public function index(){
$this->set('languages', $this->Contact->Language->find('list',array('fields' => array('Language.id', 'Language.label_en'))));
echo pr($this->data);
}

Categories