4

I was wondering about how to add the autocomplete JQuery UI widget to a form I'm developing in the Zend Framework without using ZendX. The folders for the website are set up per the framework, but I'm not using Zend_Form.

So I stripped everything down to the simplest form, which works:

<script>
  $(document).ready(function() {
    $("input#autocomplete").autocomplete({
    source: ["best", "buy"]
});
  });
  </script>

<input id="autocomplete" />

But I have a PHP file that returns entries from a database in JSON. How do I use that instead? I tried replacing the array with the name of the file, but then nothing happens. Thanks!

4
  • Are you getting any Javascript errors in the browser? Commented Jun 12, 2012 at 19:17
  • I don't think so. The pages loads the same as before. :/ Commented Jun 12, 2012 at 20:26
  • When you view the source of the rendered Zend_From page does it look like the path to the js file(s) is correct? Commented Jun 12, 2012 at 20:34
  • Please provide code sample if you can. Commented Jun 12, 2012 at 22:10

1 Answer 1

3

this should work for you:

// js stuff
$( "input#autocomplete" ).autocomplete({
    source: "http://localhost/application/index/autocomplete"
});


//IndexController.php

/**
 * Return AutoComplete stuff
 */
public function autocompleteAction()
{
    // disable view and layout, we want some fanzy json returned
    $this->_helper->layout()->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(true);

    $values = array('best', 'buy');
    $valuesJson = Zend_Json::encode($values);
    echo $valuesJson;
}

You could altough pass the BaseUrl to your Script (instead of using fullpath)

//layout.phtml
<script type="text/javascript">
var baseUrl = "<?= $this->baseUrl(); ?>";
</script>

So you could do:

source: baseUrl + "/index/autocomplete"
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! :) i think writing out the whole source path did the trick!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.