views:

796

answers:

3

I am implementing an auto complete box using the Ajax.autocompleter method of the scriptaculous.js framework.

This is the auto complete box and the div where the auto suggested entries are populated.

<?php echo $form->create('Share', array('url' => '/forms/share')); ?>
    <label for="shareWith">Share Form with</label>
    <input type="text" id="autocomplete" name="autocomplete_parameter"/>
    <div id="autocomplete_choices" class="autocomplete"></div>
    <input type="hidden" id="sharedUserId" name="sharedUserId"/>
<?php echo $form->end('Share');?>

This is the JQuery function to get the auto-suggested list and to get the id of the selected entry which is stored in the hidden field of the form.

new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
                       "http://localhost/FormBuilder/forms/autoComplete",
                     {  
                            tokens: ',',
               afterUpdateElement : getSelectedId
                           }
                       );

function getSelectedId(text, li) {
     $("#sharedUserId").val(li.id);
}

Suppose if I select multiple entries,how to send those values? Can I have an array as a hidden field, so that I can have an array of the selected elements and save that array as a hidden field?

A: 

Not related to your question though..

http://docs.jquery.com/Plugins/Autocomplete

  • jQuery Auto complete you
msarathy
A: 

Simply create a new hidden input field for every selected ID, and make sure that for each you have name="sharedUserId[]". This doesn't follow the CakePHP form element naming convention, but it will make sure that the POSTed value of sharedUserId is an array.

Robert P
A: 

serialize with json and parse it in the server back. PHP 5.2 can parse json natively.

Rodrigo