# CakePHP 5 complex search (with and without AJAX)

**URL:** <https://discourse.cakephp.org/t/cakephp-5-complex-search-with-and-without-ajax/12172>\
**Category:** Need Help\
**Created:** [August 9, 2024, 8:22pm UTC](https://discourse.cakephp.org/t/cakephp-5-complex-search-with-and-without-ajax/12172 "2024-08-09T20:22:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![only4r](https://avatars.discourse-cdn.com/v4/letter/o/45deac/32.png) [@only4r](https://discourse.cakephp.org/u/only4r)\
**Post date:** [August 9, 2024, 8:22pm UTC](https://discourse.cakephp.org/t/cakephp-5-complex-search-with-and-without-ajax/12172/1 "2024-08-09T20:22:51Z")

</div>

Hey,

I did this: [CMS Tutorial - Creating the Articles Controller - 5.x](https://book.cakephp.org/5/en/tutorials-and-examples/cms/articles-controller.html)

```auto
CREATE TABLE articles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    slug VARCHAR(191) NOT NULL,
    body TEXT,
    published BOOLEAN DEFAULT FALSE,
    created DATETIME,
    modified DATETIME,
    UNIQUE KEY (slug),
    FOREIGN KEY user_key (user_id) REFERENCES users(id)
) CHARSET=utf8mb4;

CREATE TABLE tags (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(191),
    created DATETIME,
    modified DATETIME,
    UNIQUE KEY (title)
) CHARSET=utf8mb4;

CREATE TABLE articles_tags (
    article_id INT NOT NULL,
    tag_id INT NOT NULL,
    PRIMARY KEY (article_id, tag_id),
    FOREIGN KEY tag_key(tag_id) REFERENCES tags(id),
    FOREIGN KEY article_key(article_id) REFERENCES articles(id)
);

```

How to add search form to index action?

I would like:

- Published or not (radio button);
- Input text search, LIKE or better idea in the title and body;
- Choose the tags (select).
- Order by tags count.

View file (templates/Articles/index.php):

```auto
echo $this->Form->create($article);
echo $this->Form->control('published', ['type' => 'checkbox']);
echo $this->Form->control('keywords');
// Tags from controller, how to?
echo $this->Form->select('order', ['asc' => 'A-Z', 'desc' => 'Z-A'], ['default' => 'asc']);
echo $this->Form->end();

```

How does this work on the controller side? I would like know AJAX (with jQuery or another) and without AJAX.

Thank you very much! 🙏

---

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [August 10, 2024, 7:04am UTC](https://discourse.cakephp.org/t/cakephp-5-complex-search-with-and-without-ajax/12172/2 "2024-08-10T07:04:31Z")

</div>

To begin with, implement the cakephp search plugin. [GitHub - FriendsOfCake/search: CakePHP: Easy model searching](https://github.com/FriendsOfCake/search)

---

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [August 10, 2024, 7:16am UTC](https://discourse.cakephp.org/t/cakephp-5-complex-search-with-and-without-ajax/12172/3 "2024-08-10T07:16:52Z")

</div>

+1 for this - very easy to implement

---

<div class="post-metadata">

**Author:** ![Rods](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rods](https://discourse.cakephp.org/u/Rods)\
**Post date:** [August 12, 2024, 2:20am UTC](https://discourse.cakephp.org/t/cakephp-5-complex-search-with-and-without-ajax/12172/4 "2024-08-12T02:20:15Z")

</div>

you need to loop the data in your index first, then check if there`s a query params that contain keywords
