Hi guys
I'm having problems with Ajax and the form api. I have a module that uses Ajax to generate content dynamically into a div!
My module structure looks like this
Module-> friends.module - friends.pages.inc
its also holds a directory called inc which has the following file friends.api
Now I have my main function that does this:
function friends_default() {
drupal_set_html_head('
');
return t('
');
}
When clicking the on Invite A Friend it calls the following function from friends.pages.inc
function friends_invite_form() {
global $user;
$path = drupal_get_path('module', 'friends');
require_once($path .'/inc/friends.api.inc');
print friends_invite_form();
}
function friend_invite_form(){
global $user;
$form['from'] = array(
'#type' => 'item',
'#title' => t('From'),
'#value' => t("$user->name <$user->mail>"),
);
$form['send_to'] = array(
'#type' => 'textfield',
'#title' => t('To'),
'#description' => t("(use commas to separate emails)"),
'#required' => true,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#rows' => 4,
'#description' => t("(optional)"),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Invite"),
);
return $form;
};
Now the problem I'm having is that it renders the form properly but when submit my form it escapes the div and goes back to printing the form.
Does anybody know a workaround for this. I'm planning on releasing this as an open source module for drupal as the current friend module is cool but is lacks allot of functionality.
Comments
Should'nt you use drupal_get_form ?
Instead of print ...
The problem is not with
The problem is not with print, I can do this with
friend.pages.inc
function friends_invite() {
global $user;
$path = drupal_get_path('module', 'friends');
require_once($path .'/inc/friends.api.inc');
$form = drupal_get_form('friends_invite_form');
print $form;
}
The problem lies with the submitting and validating function when I submit the form it escapes the div and displays the function friends/invite
into the menu. I'm loading the function friends_invite() into a div which calls a url ?q=friends/invite and prints the form and when I submit it kicks out of the div and goes back to the menu ?=friends/invite which just prints the the form!
In Open Source I Trust! Everybody Else Must Pay Cash!
Clarity On Issue
I have a module that is using css to display tabs like on my default page like this:
Friends| Whos Online| Requests | Invite A Friend
Now I use AJAX to generate content and it displays into a div. The content is controlled by drupal function and menus
I.E Invite A Friend -> Menu - friends/invite from friends.module ->Function - invite friends_invite() from friend.pages.inc
Menu friends/invite the loads into a div on ?q=friends menu with ajax
the friends invite function looks like this now:
function friends_invite() {
global $user;
$path = drupal_get_path('module', 'friends');
require_once($path .'/inc/friends.api.inc');
$form = drupal_get_form('friends_invite_form');
print $form;
}
which loads this into the div from friends/inc/friends.api
function friends_invite_form($form_state) {
global $user;
$form['#redirect'] = array('friends', 'destination=friends');
$form['from'] = array(
'#type' => 'item',
'#title' => t('From'),
'#value' => t("$user->name <$user->mail>"),
);
$form['send_to'] = array(
'#type' => 'textfield',
'#title' => t('To'),
'#description' => t("(use commas to separate emails)"),
'#required' => true,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#rows' => 4,
'#description' => t("(optional)"),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Invite"),
);
return $form;
}
Now Everything displays fine but when submitting the form instead of displaying the error on ?q=friends it jumps to my form rendering function ?q=friends/invite/
Is there a way I can redirect the errors and submission to display on ?q=friends
In Open Source I Trust! Everybody Else Must Pay Cash