I have an auto complete box which is populated with the list of users of the application. It is working fine with the box listing the users.
But I am able to select only one user. How to select multiple users from the list ?
And also how to save the selected user's names in a variable or an array?
EDIT
I am using the built-in auto complete feature of the CakePHP framework. This is the action in the controller which generates the auto complete text box.
function autoComplete()
{
$this->set('users',$this->User->find('all',array(
'fields'=>array('User.id','User.name'),
'conditions'=>array('User.name LIKE' => $this->data['User']['name'].'%'))));
$this->layout = "ajax";
}
This is the auto_complete.ctp file
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user['User']['name']; ?></li>
<?php endforeach; ?>
</ul>
And this is the view where I have the auto complete box:
<?php echo $form->create('User', array('url' => '/forms/share')); ?>
<?php echo $ajax->autoComplete('User.name', '/forms/autoComplete');?>
<?php echo $form->end('Share');?>
In the auto complete box, I am able to select only one user name. how can I select multiple users with a comma or space separator?
I don't think the AjaxHelper can produce a multi-selection auto-complete box, it's not what it's designed to do. I'm afraid you'll have to roll your own solution. Since you're already getting a nice list via Ajax that shouldn't be too much trouble.
If you want something like the Stack Overflow tag box you can probably get by by placing a few Javascript callbacks in the Helper, if you're looking for a checkbox based list you'll need to do your own.
Related
I'm trying to fetch a common element from my admin.ctp layout
<?php echo $this->fetch('my_element'); ?>
This functions is working properly in my default layout but is returning an empty string if called from my admin layout.
I'm using admin routing prefixes.
Can you use this form:
<?php echo $this->element('my_element'); ?>
Beginners question: Why is cakePHP not displaying the carriage return/new line and other characters when using;
<?php echo h($property['Property']['fullDesc']); ?>
I tried using
<?php echo $property['Property']['fullDesc']; ?>
but both showing output of text as one block of text instead of paragraphs.
Any help much appreciated.
The h function is just a wrapper for the php htmlspecialchars() function. It will not convert carriage returns into <br /> tags. You will need to do something like this:
echo nl2br(h($property['Property']['fullDesc']));
basic PHP...
<?php echo nl2br(h($property['Property']['fullDesc'])); ?>
nl2br() will form those newlines into <br>
Tip: you can make your bake templates include that automatically for all your textarea fields.
See http://www.dereuromark.de/2012/04/24/cake-bake-custom-templates-deluxe/
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
I store tags in a table which is entered by users of my site. So basically I just need to run,
<?php echo $thing->tags; ?>
and this will return all the tags for that thing. Right now it just spits out a list of the tags like "bananas apples fruit, blueberries", where the tags are both comma and space delimited. I know that in order to create a tag cloud I need to spit out the tags to a ul and then run a jquery script on the li's. I think if I can get my tags to appear in the form,
<ul>
<li>bananas</li>
<li>apples</li>
<li>fruit</li>
<li>blueberries</li>
</ul>
Then I will be able to write a jquery script to put each of these elements in little tag cloud boxes. But I don't know how to get the tags to appear in this form. When I echo the tags can I apply some php function which will do this for me?
UPDATE: I used preg_split,
<?php $tags=preg_split("/[\s,]+/", "$things->tags"); var_dump($tags);?>
And now they are listed in an array -- comma and space delimited. My only challenge now is to surround these elements of the array in li tags.
Okay, I figured this one out on my own. After creating the array as mentioned above, I used
<?php foreach ($tags as $row) : ?>
<li><? echo $row[0]; ?></li>
<li><? echo $row[1]; ?></li>
<li><? echo $row[2]; ?></li>
<? endforeach; ?>
etc.
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