Reload Element after ajax call

Hello,
Is there a way to reload a specific element of my view, after a ajax call? (i’m using version 3.5)

What I’m doing:

  1. edit.ctp
<?php echo $this->element('Conversion/phones'); ?>
  1. Element/Conversion/phones.ctp
<?php if (isset($phone)) echo $phone; ?>
  1. ConversionController.php

public function ajaxEdit()
{
$this->viewBuilder()->setLayout(‘ajax’);
$this->set(“phone”, “3199999999”);
$this->render("/Element/conversao/contatos");
return;
}

Expectation: the specific element “phone”, reload.

Reality: “Undefined variable: phone”

NOTE: I use ajax perfectly, but when I try to load via ajax ONLY a specific element, I can’t.

The solution i’m using is:

  1. Build the HTML inside the Controller
  2. In Ajax sucess: $(‘my_element’).html(response.html_from_controller)

But this solution is a little ugly…
is there another way?

Thanks a lot!

I think that’s it. PHP is the server side html generator, so once the page is loaded its all back to you to control. You could put it in an iframe driven by another controller/action, and refresh it. That allows you to design the page layout back in the PHP. It also means the AJAX reply need only be “Done!” and the refreshed iframe will build from the CakePHP. There may be a cleverer way, but that’ll work.

Thanks @Jawfin !
yes…it works!

But I hope Cakephp has a better way of doing this.

Anyway I will build my HTML inside my CONTROLLER… and then reload the HTML element in my view (using JQuey).

But II really think that building the HTML inside the CONTROLLER is not the best way.

Thanks for your help!.

You should definitely not be building your HTML in the controller. Cake supports Ajax views, which you can use for this purpose.

1 Like

Thanks Zuluru,
Yes, I agree with you.

But how can I render a part of the View (ie: an Element)?

When I return a JSON, using AJAX, I can “rebuild” the view using javascript. Nice.

But I want to do it using cakePHP Helṕers, Cells or Elements.
I read the docs, para I didn’t find the answer.

Thanks

It’s not clear where you’re stuck.

You have a page, which has a template, which is created by using a number of elements? And now you want to update the content of just one section, which is generated from a particular element? Your Ajax call would presumably have a template which simply outputs a single element.

If your situation isn’t quite like this, maybe this is still enough to get you on the right track?

1 Like

Hi Zuluru,
I did it! Thanks a lot for your help.

1 Like