Hello everyone,
I need help in adding a piece of code to the user-edit form in drupal when submit button is clicked.
Basically the form (user registration) has 2 checkboxes and depending on which checkbox the user has selected particular functions need to be called when the submit button is clicked.

I read that it is possible but dont know how.

Can anyone please explain this .

Thanks.

Moved by VM

Comments

marcvangend’s picture

You can write a custom module, implement hook_form alter and add a submit handler to the form:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'my_form_id'){
    $form['#submit'][] = 'mymodule_mysubmit';
  }
}

function mymodule_mysubmit() {
  // your additional submission handling code
}

see http://api.drupal.org/api/function/hook_form_alter, http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/6 for more info; keep asking if this sounds like abracadabra to you.

eng.anas’s picture

i have the same case but in my situation, when the user press submit in CCK it exec a Perl script, so how could i join between the submit form and the exec function, and also i need to exec different function on different cck, for example i made a cck for create category, when i press submit it should exec a function, on the other hand i create a cck for products, so when submitting it should exec the function of product not the category one, so what is the way to do that.
thanx a lot

marcvangend’s picture

Sorry, I can't answer that.
- You're hijacking someone else's topic
- I know nothing about perl
- I don't understand your question because you glued all your thoughts together in one long sentence

aamin’s picture

Let me try it, If I questions I will ask here.
Thanks for the prompt response

eng.anas’s picture

marcvangend :
i put my issue here http://drupal.org/node/490782

phelix’s picture

I am using this and it is working however, the form that i am modifying is basically when you edit a node of a custom content type. The function that i am trying to use gets information from the database that one of the fields on this form submit. It seems that when I add or modify an item on this form and then submit my changes do not display the first time. I have to go back into the form and submit it again and then it works. Is there something else that I must do to have it view or modify the items that were just submitted by that form on the first submit? Hope this makes sense.

aamin’s picture

Thanks for the help,

I have created a content type called checkboxes which contain two checkboxes newsletter 1 and newsletter 2. now based on the checkbox checked while creating content I want to add a piece of code when the submit button is clicked.

I found out the sample code for the module (for Drupal 5.x) but have problems in customizing it work with my data. I have included details of the form which consists of the checkboxes, details of the checkboxes (id,name etc) and of the Submit button.

I will really appreciate if you can guide me as to how to replace the variables in the code below to customize it to my needs.

?php
function hook_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_settings' == $form_id) {

// code to be executed
}
}
?>

Form Information

form id="node-form" enctype="multipart/form-data" method="post" accept-charset="UTF-8" action="/dev/drupal5/node/add/checkboxes">

Checkbox Information

div class="form-checkboxes">
div id="edit-field-option1-keys-Newsletter-1-wrapper" class="form-item">

input id="edit-field-option1-keys-Newsletter-1" class="form-checkbox" type="checkbox" value="Newsletter 1" name="field_option1[keys][Newsletter 1]"/>
Newsletter 1

div id="edit-field-option1-keys-Newsletter-2-wrapper" class="form-item">
label class="option">

Newsletter 2

Submit information

input id="edit-submit" class="form-submit" type="submit" value="Submit" name="op"/>

Thanks a lot.

marcvangend’s picture

Sorry, I don't really understand what you're trying to achieve. (It would also help if you put your code in proper code tags.) What is the role of that content type here? Can a user create a checkboxes-node, and do you want to do something when the node is submitted? In that case, you'd better use hook_nodeapi.

aamin’s picture

hi,
sorry for the confusion. The content type is just for testing purposes. There are two checkboxes indicating newsletters the user would like to subscribe to. based on the option selected by the user (For testing purposes, I create new content) a code has to be executed -( for eg if a user checks both the boxes then on clicking the submit button it should run a piece of code that makes sure that the user receives both the newsletters )

I hope this helps u understand.

Your help is appreciated.

Thanks.

marcvangend’s picture

OK, if I understand correctly, you are adding these checkboxes to a form using hook_form_alter. Can you help me understand some more about your situation? Where did the hook_form_alter go - did you create a module? Can you post the complete code? Which form are you altering? Where and when will you present these two checkboxes to the user?

aamin’s picture

No, I have still not created a module, so as of now I dont have any code to show. I want to know how should I go about it. The user will be presented with these checkboxes at the time of registration and whenever he would like to modify his profile settings. I guess it is user-edit form and user-register forms (though I dont know about it). I tried going through the form- API but could not understand much. that is why I dont know how to create the module that will help me.

Thanks.

marcvangend’s picture

IMHO, the best way to learn is to start trying and learn while you work. If you only want to start after I have explained everything, you are basically asking me to do all the work. No offense but that's not how it works.

There are a lot of modules already that allow you to add things to the user pages, like Profile (in core) and Content Profile (http://drupal.org/project/content_profile). Simplenews (http://drupal.org/project/simplenews) is a newsletter module that also integrates with the user pages. I don't know which option is best for you; that also depends on what you want to do with the information after the form is submitted.

I advice you to get started now, try it all out, maybe experiment with the form api and custom modules, and come back when you have more specific questions.