Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By I am learning on
Hi,
Using Forms API, how to create a button (not submit) to call a function or to redirect the user? I understand the submit and it works fine but could not figure out how to create a button that performs an action like redirect or call a function?
Thanks and Regards
Comments
They are one and the same.
They are one and the same. You create the submit button, and in the submit function you set your action (redirect/call a function).
It means I have two instead
It means I have two
Did I get it right?
Yes (though you should show
Yes (though you should show your form API definition, not the HTML).
Then you will check which one was clicked in your $form_state['values']['op'], and perform the action accordingly.
function
This: <?php
This:
Should be this:
Then in your submit function, you would do this:
Also, in the future, when posting code, try to scale it down to the relevant code. No code is no good, but so is too much code! In this case it wasn't a big deal, but other times it will reduce your chances of getting a response, as people often don't want to dig through lines and lines of code to find the relevant parts. If you haven't provided enough, people will ask for more.
I thought it will help, I'll
I thought it will help, I'll be specific next time :-)
I'm just checking your suggestion
canceling this returns back
canceling this returns back to the forms
I checked the
$form_state['values']['op']
but didn't find key 'op'Whatever you are showing
Whatever you are showing there, it's not $form_state['values'] - the form_id is not part of $form_state['values'].
This is the output of the
This is the output of the following I tried:
Is my implementation is wrong?
No, it turns out I was wrong
No, it turns out I was wrong - the form_id is a part of $form_state['values']. But so is $form_state['values']['op']. Did you configure it as type 'submit' or type 'button'?
Submit, as you suggested.
Submit, as you suggested.
Show your FAPI code again
Show your FAPI code again then.
//Create a Cancel Button
Try removing this: <?php
Try removing this:
'#name' => 'cancel',
Then check again whether 'op' is set or not.
Also, do you have any javascript set on this form that is set to submit the form?
Removing this worked, and I
Removing this worked, and I got 'op' as well, Thank you so much.
Regards
No problem. Glad it worked!
No problem. Glad it worked!
something like this
something like this perhaps?
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
//Create a Cancel
Clicking on Cancel doesn't invoke this function and it remains on the form.
What do you want to achieve
What do you want to achieve for the cancel button should be coded in
essentially you got 2 different button, one is the "cancel" and the other is the "submit" button, the "cancel" button will look for the "ca_designation_list" function and the "submit" button will search for the _submit function (because you haven't specified special '#submit' value in the form array)
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
This button will be on a Form
This button will be on a Form to edit the existing record, so cancel will take the admin / user back to the listing page. But it can be for any purpose e.g. delete the current record, so call a function to perform so. I'm not able to call a function on a button. An example will be a great help.
Thanks
Thanks Jay, you have been a
Thanks Jay, you have been a great help.
This one is for anybody looking for the same, this is how it is implemented (Courtesy Jay)
In you form:
and in your submit:
Thanks Jay.