Drupal: How do I modify form ui after 'Remove' button pressed using AJAX?
I am using Drupal 6 and have the AJAX module installed. I have the following code:
function remove_manufacturer_role_form($form_state) {
$form['#ajax'] = array(
'enabled' => TRUE
);
$form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
$form['submit'] = array('#type' => 'submit', '#value' => t('Remove yourself as manufacturer'));
return $form;
}
function remove_manufacturer_role_form_submit($form, &$form_state) {
$current_vars = ogrolerequestmanufacturerblock_get_current_vars();
if( $current_vars )
{
$curr_gid = $current_vars['current_gid'];
$curr_uid = $current_vars['current_user_id'];
$delete_query = "DELETE FROM {og_users_roles} WHERE rid in (SELECT rid FROM {role} WHERE name='Manufacturer') AND uid=$curr_uid AND gid=$curr_gid";
if( db_query($delete_query) )
drupal_set_message("You successfully removed yourself as a manufacturer from this project");
}
}
Basically, I have a form to remove a user as a manufacturer within a project. The u开发者_JS百科ser has the ability to remove themselves. This form allows the user to click a button, which in turn uses AJAX to submit the form, delete the user's role as manufacturer from the database, and notify the user whether it was successful or not. It successfully makes the call using AJAX and deletes the role like it should, but the form still has the remove button shown on it. If the user clicks the button again it gives an error b/c there is nothing to delete. This is OK, I guess, b/c it is not a fatal error and doesn't really affect anything ... however, it's not pretty for the user.
Is there a way I can refresh the module's block or modify the form that is shown once it is submitted?
Edit:
This seems like a good solution, but I can't quite seem to get it working. I've included the delete.js javascript within my module:
/**
* Ajax Forms plugin for ogrolerequestmanufacturerblock
*
* @param {String} hook
* @param {Object} args
* @return {Bool}
*/
Drupal.Ajax.plugins.ogrolerequestmanufacturerblock = function(hook, args) {
alert('it got into javascript!');
if (hook === 'submit') {
alert('submit called within javascript!');
}
return true;
}
It never seems to get into this javascript function. How does drupal know to call this specific function?
You can check the return value coming through AJAX and if the removal has been successful, you can do a display: none
on the remove button.
Looking at the AJAX module documentation, I think you will need to implement Drupal.Ajax.plugins.FOO(hook, args)
. When hook == complete
, you can set your button to display: none
.
精彩评论