CakePHP: Using FormHelper to pass search query to controller parameter - php

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.

Related

Cake PHP Form getting data from input and passing it to the url

I have my view controller that grabs a code number from the url
users/view/10024 or users/view/10004 etc
in my user index.ctp
echo $this->Form->create(null, array(
'url' => array('controller' => 'users', 'action' => 'view2'),
'type' => 'get',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
echo $this->Form->input(
'Enter Code',
array('label' => 'Code')
);
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
)
);
I get this
/users/view?Enter+Code=1001
I want
/users/view/1001
So i want to grab the input data and put it in the url
This cannot be done using PHP.
Instead you need to use the rewriting engine of Apache: MOD_REWRITE http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Create a simple rewrite rule that transforms the URL in the way you want to achieve.
You can see an example on rewriting URL's here: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Cakephp two forms (sending data to different controller)

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?

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);
}

cakePHP formHelper postLink image

I want to output an image with a hyperlink wrapped around it instead of just a text hyperlink using cakePHP's formHelper::postLink function.
Does anyone know how to do this? I tried multiple things but couldn't get it working.
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $country['Country']['id']),
array('confirm' => __('Are you sure you want to delete ').$country['Country']['name'].'?')
)?>
So instead of 'Delete' I want to display an image.
Here's what works for me.
echo $this->Form->postLink(
$this->Html->image('icn_trash.png', array('alt' => __('Effacer'))), //le image
array('action' => 'delete', $artist['Artist']['id']), //le url
array('escape' => false), //le escape
__('Êtes-vous sûr de vouloir effacer artiste #%s?', $artist['Artist']['id']) //le confirm
); //le voila
Try this :
echo $this->Form->postLink(
$this->Html->image('delete.png',
array("alt" => __('Delete'), "title" => __('Delete'))),
array('action' => 'delete', $items['Item']['id']),
array('escape' => false, 'confirm' => __('Are you sure?'))
);
If I understand your question correctly I dont think you want to use $this->Form->postLink
I think this page is exactly what you're after: http://book.cakephp.org/view/1441/image
This uses $this->Html->image to create the image and then you can pass a URL through as one of the parameters to specify the surrounding anchor link.
You can wrap in image inside a link element, but you need to set the escape option to false, like this:
echo $this->Html->link(
$this->Html->image('your_image_here.jpg', array(
'alt' => 'Alternative Text for your image',
'title' => 'Optional tooltip text for your image'
),
array(
'controller' => 'YourController',
'action' => 'someAction'
),
array(
'escape' => false // Add this to avoid Cake from printing the img HTML code instead of the actual image
)
);
That should do the trick.
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array("alt" => "Brownies")),
array(
'controller' => 'recipes',
'action' => 'view',
'id' => 6,
'comments' => false
)
)
function delete_image(){
if ($this->Session->read('Auth.User.id')) {
$this->User->id = $this->Session->read('Auth.User.id');
$this->User->updateAll(
array('User.image' => "''"),
array('User.id' => $this->User->id)
);
$this->Session->setFlash('The image has been deleted.');
$this->redirect(array('action' => 'profile'));
}
}

Categories