Ajax edit form load into a DIV

diego_miola - March 26, 2007 - 13:41

Using Ajax, I need to load an edit form into a DIV, so a user can edit part of his profile, but the request brings the whole system. I can't get only the form. For example, in Cake using $this->layout = 'ajax', you can get only the content of index(). Anyone knows a solution for this problem ?, here is my code:

/* editprofile.js */

$jq(document).ready(function(){
//var base_url = $jq("#base_url").val(); //traigo la url absoluta del site.

$jq("#edit-personal").click(function(){
//var personal_info_url = base_url + '/node/' + $jq("#personal_info").val() + '/edit';
alert(personal_info_url);
$jq("#personal").load("?q=editprofile/edit_personal",
function() { alert("Edicion de Perfil Cargado"); }
);
return false;
}
);
});

thanks and sorry for my english.

Diego.

RE: Ajax edit form load into a DIV (corrected code)

diego_miola - March 26, 2007 - 13:51

Sorry, I've made a mistake in the original post. This is the correct code.

/* editprofile.js */

$jq(document).ready(function(){
var base_url = $jq("#base_url").val(); //traigo la url absoluta del site.

$jq("#edit-personal").click(function(){
var personal_info_url = base_url + '/node/' + $jq("#personal_info").val() + '/edit';
//alert(personal_info_url);
$jq("#personal").load(personal_info_url,
function() { alert("Edicion de Perfil Cargado"); }
);
return false;
}
);
});

The path node/{nid}/edit returns a complete page

nevets - March 26, 2007 - 14:07

The path node/{nid}/edit returns a complete page and not just the form. You would need a path the returns just the form though I am not sure how well the form API and this will play together

RE: The path node/{nid}/edit returns a complete page

diego_miola - March 26, 2007 - 14:13

thanks nevets,

Yes, that's right, node/{nid}/edit returns a complete page, but I can't find the way to load only the form. The node was made using CCK and is really simple, just three fields.

So now, the question is... how to get through AJAX a $content without getting the whole page...?

I used a bit of hack to do a

ukdg_phil - March 26, 2007 - 14:41

I used a bit of hack to do a similar thing with a shopiing cart edit form in the past.

I embeded the form in the normal page & hid it using css. Then pulled the contents of the hidden div into a JS pop-up so the user could do their thing, & re-hid it after they finished. Not AJAX based at all, as the whole page was submitted & reloaded after the edit.

This worked in my instance, but I wouldn't really recommend it, & probably won't suit you, but it depends on the type of site & situation you need it for. This was a small-ish site with low traffic, so it wasn't worth my time trying to figure out a more elegant way of doing it.

Ideally it would be nice for a contrib-module/core-update to allow certain chunks of a page/block etc. to be called for use in AJAX type situations, not sure if anyone is working on this at the moment?

Anyway, just thought I'd throw in my 2 cents worth...

Regards,
Phil.

UK Digital Graphics Ltd (ukdg.net)

I've found a solution !

diego_miola - March 26, 2007 - 17:42

Using the pagearray module we can any part of the node: $title, $breadcrumb..and $content !!. Like the description says: pagearray is a helper module to load page output as an array.

So we need to pass a path to the ajax callback function and do this: (very simple example...but works...)

<?php
function ajax_callback($path){
 
$return = module_invoke('pagearray', $path);
  print
$return['page']['content'];
}
?>

This is the link to the module:

http://ftp.osuosl.org/pub/drupal/files/projects/pagearray-5.x-1.x-dev.ta...

thanks...

Hey diego, Do you have a

mokargas - January 2, 2008 - 00:17

Hey diego,

Do you have a working example of this? I'm using pagearray, but ajax submission doesn't work?

Sorry - totally missed this

ukdg_phil - January 11, 2008 - 14:38

Sorry - totally missed this post when you originally made it diego!

I'm just starting work on a new site that could possibly benifit from the pagearray module, although haven't had a chance to look into it yet.

Thanks for the info ; )

UK Digital Graphics Ltd (ukdg.net)

Correction

zorroposada - February 25, 2008 - 07:53

Thanks for this code sample but I believe you have an error there

It should be like this:

<?php
function ajax_callback($path){
 
$return = module_invoke('pagearray','page', $path);
  print
$return['page']['content'];
}
?>

According to http://api.drupal.org/api/function/module_invoke/5 you need to tell ajax_callback the hook you want to use. In this case you need to tell pagearray to use the page hook.

I created a module and used hook_menu to map a drupal url to a function that prints out the part of the node that I wanted to show, it work perfect.

A godsend, this

rubymuse - March 6, 2008 - 21:15

Thank you all for this, including the author of pagearray. That little function is incomprehensible to me but sure did the trick.

You can probably also create

criznach - March 7, 2008 - 04:05

You can probably also create a page template for your form page that contains just this code:

<?php
print $content
?>

Then you'll just get the form.

See this page for references on themeing a specific page with a different template.

http://drupal.org/node/104316

 
 

Drupal is a registered trademark of Dries Buytaert.