PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] CakePHP 3.6.14: Render view (.ctp) in a bootstrap modal

 February 17, 2022     bootstrap-modal, cakephp, php     No comments   

Issue

Is it possible to display a view (add.ctp for example) in a bootstrap modal? And if so is it possible to render only the html in the add.ctp file without loading the default layout?

Because currently I am trying to build a custom form similar to the one in the add.ctp file in order to display it in the modal, and its really a pain trying to post and get the json objects in order to submit the form and populate the grids in my application.


Solution

Yes it`s possible.

Create add.ctp in Ajax folder, for example:

/Posts
  index.ctp
  /Ajax
  add.ctp

in Postings::add() set Ajax layout and with js get /posts/add and render modal.

Read:

https://book.cakephp.org/3.0/en/controllers/components/request-handling.html https://book.cakephp.org/3.0/en/views.html#layouts

EDIT:

in Controller

public function add()
{
   // your code here ...
   if ($this->getRequest()->is('ajax')) {
       // render "add" view in Ajax folder and use "ajax" Layout
       $this->render('Ajax/add', 'ajax')
   }
}

https://book.cakephp.org/3.0/en/controllers.html#rendering-a-specific-template

EDIT 2 (jQuery part) example

<button type="button" data-toggle="modal" data-remote="<= $this->Url->build(/* ADD HERE YOUR PARAMS*/) 
 ?>" data-target="#myModel">Open Model</button>

$('body').on('click', '[data-toggle="modal"]', function(){
        $($(this).data("target")+' .modal-body').load($(this).data("remote"));
    });  


Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,204,840

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing