Cakephp how to create a simple search form - cakephp

I need some help to create a simple search form with one input box. It needs to be able to send a value to a controller in the url like so:
http://mysite.co.uk/properties/results?search=northampton
I would like to use the cakePHP form helper, but can you include an input that doesnt relate to a column in the database?

have since found the answer to this:
///in the view:///
<div class="container">
Property Search</div>
<div class="col-md-12 purple search_box">
<?php
echo $this->Form->create('Properties', array('type' => 'get'));
echo $this->Form->input('search', array('between'=>'<label for="search" class="main_search">Search</label><br>','label'=>false));
echo $this->Form->button('Search', array('class'=>'btn btn-success'));
echo $this->Form->end
?>
</div>
</div>
//in the controller//
if(isset($this->params['url']['search'])){
echo 'search text has been found';
}
Fair comment burzum

Related

Render views with actions from one controller into a view of a different controller in CakePHP

I'm trying to render a view that with actions from one view, into another controllers view.
I have a a view called stocks.ctp (using StocksController) that I want to render in home.ctp (HomeController) I thought I could just do <?php echo $this->fetch('../Stocks/stocks'); ?> in home.ctp, but that isn't working.
I've also tried putting <?PHP $this->extend('stocks'); ?> in home.ctp but that just overwrites the entire view and only renders stocks.ctp.
How can I display stocks.ctp inside home.ctp?
This is just an an example: Here is the
<?php $this->start('stocks');?>
<div>
content
</div>
<?php $this->end(); ?>
And here is just and example of the home.ctp view.
<div class="col-3">
<h1>STOCK MARKET WOOOOO</h1>
<?php echo $this->fetch('stocks'); ?>
</div>
<div class="col-3">
<h1>Opinion</h1>
<hr></hr>
</div>
How can I render this properly? I've tried a bunch of solutions that I've found here but nothing has worked yet and I've tried the using cookbook's documentation but no luck. I can't figure out what I'm doing wrong.
You should read about how to create elements in CakePHP documentation.
After create elements, you should be able to use them wherever you want in your application.
Create the element inside View/elements, ex: /View/elements/stocks.ctp
Request it in your home.ctp.
For example:
<?php
echo $this->element('stocks');
?>
The element should be rendered.
Read about it here: Elements
You should put this in your Homecontroller..
$this->render('Stocks/stocks');
In your controllers action do
$this->autoRender = false;
and at the end of your controller action do
$this->render('Stocks/stocks');

My web page redirects to a blank screen on submitting the form

I am trying to display a flash message saying 'Mail sent' on sending an email from a form in cakephp. The email is send successfully to the recipient but then it goes to a blank screen and doesn't display the flash message which indicates that the message has been sent.I have checked for spaces before and after php tags in the controller and that doesnt seem to be the problem.IT works fine on my localhost and displays the message but not on the server.I will put my controller code
public function contact() {
$email = new CakeEmail();
if ($this->request->is('post')) {
$this->Contact->set($this->request->data);
if ($this->Contact->save($this->request->data)) {
//$this->Project->set($this->data)
$name = $this->request->data['Contact']['name'];
$mail = $this->request->data['Contact']['email'];
$email->from(array($mail => $name));
$email->to('sales#blacknova.com.au');
if (isset($this->request->data['Contact']['contacttime'])) {
$ctime = implode(',', $this->request->data['Contact']['contacttime']);
}
$message = "Phone No : ".$this->request->data['Contact']['phone']."\n\nBest Contact time :".$ctime." \n\nMessage : ".$this->request->data['Contact']['description'];
$email->subject('BlackNova Website Contact Form Message');
//$email->send($message);
//$success =1;
//$email->send($this->request->data['Contact']['phone']);
//pr($message);
if ($email->send($message)) {
$this->Session->setFlash('Mail sent successfully');
$this->redirect(array('controller' => 'BlockContents', 'action' => 'contact'));
//pr($success);
}
}
}
}
There is a contact.ctp
<div id="formcontainer_left">
<?php echo $this->Form->create('Contact'); ?>
<div class="inputbox1"><?php echo $this->Form->input('name');?></div><br>
<div class="inputbox2"><?php echo $this->Form->input('email'); ?></div><br>
<div class="inputbox3"><?php echo $this->Form->input('phone');?></div><br>
</div>
<div id="formcontainer_right">
<div id="me"><?php echo $this->Form->input('description');?>
</div>
<?php echo $this->Form->submit('SUBMIT');?>
</div>
<div id="formcontainer_xright">
<div class="formcontainer_last"> BEST TIME TO CONTACT* </div>
<div id="chkbox">
<?php echo $this->Form->input('contacttime',array('label'=>false,'type'=>'select','class'=>'checkdiv','multiple'=>'checkbox','options'=>array('Before work' =>'Before work','During work'=>'During work','After work'=>'After work'),'escape'=>false)) ;?>
</div>
</div>
<?php echo $this->Form->end(); ?>
<div class="contactformbottom">
</div>
</div>
<div id="formcontainer2">
<p> <?php echo $this->Session->flash();?></p>
Could somebody please help me find out why it happens only on the srver and not on the local host?
The problem is that your BlockContents isn't a valid controller in the redirect url.
If you try using an underscored version block_contents instead, you'll find it'll probably work.
Take a look at the recommended usage in the book. http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#url-considerations-for-controller-names
As #DavidYell says, the problem is the controller in the redirect. If you do need that redirect, his answer should solve everything. I just want to point out you don't really need the redirect if all you want is to clear the values in the textboxes but stay on the same action.
if ($email->send($message)) {
$this->Session->setFlash('Mail sent successfully');
$this->request->data = null;
}
should clear the textboxes with no need of redirection.

How to get controller view inside an element?

I have created an element having a text field(Status Bar same as Facebook) which i extended in home/display view.
Code for element:
<?php
echo $this->Html->css('notes');
echo $this->Html->script(array('jquery','jquery.elastic','notes'));
?>
<div class="notescontainer">
<div class="share">Share:</div>
<div class="notes">Notes</div>
<div class="loading"></div>
<div class="img_top"></div>
<div class="text_status">
<?php
echo $this->form->create('Note', array('action' => 'notes'));
echo $this->form->input('notes',array('class' => 'notesbar', 'label'=>'', 'placeholder' => 'Write down you notes ...'));
?>
</div>
<div class="button_outside_border" id="share">
<div class="button_inside_border">
<?php
echo $this->form->submit('Share',array('alt' => 'Add', 'class' => 'sharebutton'));
echo $this->form->end();
?>
</div>
</div>
<div class="clear"></div>
<div class="load_status_out"></div>
</div>
I want that when user shares the status it should re-appear on the same page that is display/home and for that i created a controller for retrieving the status entered by the user.
Controller:
<?php public function notes()
{
$this->set('allnotes', $this->Note->find('all'));
}?>
Controller View:
<?php
foreach($allnotes as $viewnotes):
{
echo $viewnotes['Note']['notes'];
echo "<br>";
}
endforeach;
?>
Now i want this controller view to be displayed on home/display view. Is there any way i can extend it in that element? I don`t want to add it directly on my home/display view as it already fully loaded.
You can use requestAction() to accomplish this, but in my experience it is buggy and a sign of design failure if you need it.
Alternatively you can turn all your view into elements, which can be embedded in each other in any arbitrary way. Then you can just call some shared code in the controller to populate all required models.

Using Ajax with cakephp to upload files

I have built an cakephp site that lets users upload files, all works fine but I would like to show a image saying "file uploading" or something while it is uploading. Now I know that I should use the $ajax->form helpers but I can not seem to get this to work. Can some one point me in the right direction please
<?php echo $ajax->form(array('type' => 'post',
'options' => array(
'model'=>'Upload',
'update'=>'post2',
'action' => 'add',
'confirm' => 'Are you sure?',
'indicator' => 'loading',
'before' => '$("#post2").html("Wait a moment")'
)
));
?>
<fieldset>
<legend><?php __('Add Upload');?></legend>
<?php echo $form->input('title'); ?>
<div id="formUpdate">
</div>
<?php echo $form->input('Uploaded.uploaded_file', array('type' => 'file', 'label' => 'Upload file')); ?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
<div id="post2"></div>
Ok, if you don't want to use Ajax upload solution, then it could be achieved with simple javascript. I would explain how to do with jQuery, but you can achieve with any library or pure javascript too.
So, create a hidden div containing your please wait message i.e.:
<div id="wait">File is uploading please wait</div>
Then on the submit button of your form attach following function:
function pleaseWait(){
$('#your_form_id').hide();
$('#wait').show();
}
This basically would hide the form and will display the wait container. Once the file has been uploaded, the page will be redirected so, you don't need to worry how to show the form again.
I've did it on my project and it was working pretty well.

Help with the auto complete method in cakephp

I'm trying to have an auto complete box in my application,where the values are retrieved from a table.
I want to share a form with set of users in the Users table. i have a link called 'Share' and when I click the link, the autocomplete box is generated.
Now when I type in the text box any letter, I want it to be auto suggested.
This is my view file, /views/main/home.ctp
<?php echo $javascript->link('prototype');?>
<?php echo $javascript->link('scriptaculous');?>
<?php echo $javascript->link('effects');?>
<?php echo $javascript->link('controls');?>
$(document).ready(function(){
$(".Share<?=$r['Form']['id'];?>").click(function(){
$("#share<?=$r['Form']['id'];?>").toggle("show");
});
});
<li id="Share<?php echo $r['Form']['id']?>">
Share
<div id="share<?php echo $r['Form']['id']?>">
<?php echo $form->create('User', array('url' => '/forms/share'));?>
<?php echo $ajax->autoComplete('User.name', '/forms/autoComplete');?>
<?php echo $form->end('Share');?>
</div>
</li>
This is my auto_complete.ctp file:/views/forms/auto_complete.ctp
<?php echo $javascript->link('scriptaculous.js');?>
<?php echo $javascript->link('prototype.js');?>
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user['User']['name']; ?></li>
<?php endforeach; ?>
</ul>
And this this the function in the forms_controller: /forms/autoComplete
function autoComplete()
{
$this->set('users',$this->User->find('all',array('conditions'=>array('User.name LIKE' => $this->data['User']['name'].'%'))));
$this->layout = "ajax";
}
I get the results in the auto_complete.ctp file, i.e, if I type in the letter 's' in the autoComplete text box, I get the corresponding users in that file, but I do not get those values in the text box. Someone help me please.
Well I found the answer myself.
Autocomplete feature of cakephp does not work if you use JQuery.
I removed the line
<?php echo $javascript->link('jquery');?>
and it is working...
EDIT:
If you also want to work with jquery and need the jquery.js file, you would need to enable no-conflict mode in jQuery,as given in the site
http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Resources