By johnhelen on
Hello
How I can add a Cancel button so that when I click this button, the browser goes back to the previous page
I have a look around but could not find out
Many thanks
john
Hello
How I can add a Cancel button so that when I click this button, the browser goes back to the previous page
I have a look around but could not find out
Many thanks
john
Comments
Input?
I would like to know how to do this as well. Anyone have any input on this? It would be nice to give the user an option to back out of adding content at the form level instead of clicking a link? Could someone maybe help us out? Thanks
Me too
Is there a standard way in drupal to handle a cancel button?
add for button
add this in your template.php file under your "node_form" hook.
$output .= "<input type='button' value='Cancel' onClick='cancelAction();' />";then add this in the head section of your page.tpl.php file
Guy I work with figured this one out.
Maybe this?
A user browses to a page where they are presented with a submit changes button or cancel those changes button. Now if the user clicks on the cancel button, how do you in Drupal go back to the previous page. Perhaps this solution would work.
In the function that defines the $form variable include this snippet:
$form['cancel'] = array(
'#prefix' => '<div class="floatLeft">',
'#value' => '<input type="button" value="Cancel" id="cancel">',
'#suffix' => '</div>'
);
Then in an included javascript file, add this jQuery code:
$(function() {
$("#cancel").click(function() {
history.back(1);
});
});
I hope this helps.
Non-javascript reliant solution
I prefer to build solutions that don't have dependencies with javascript if I can help it.
In this case, when moving to the form I include a $_GET variable for the location to return to. Something like this:
Slip that variable into your form as a hidden variable so you can access it when your button is submitted.
Since when a button is pressed there is no submission handlers you need to check for that button before you render your form otherwise your required fields will complain.
Something like this:
Disclaimer: I've not copied that from working code or anything, just banged it out, so it may have a typo or two in it :)
Good solutions.
Good solutions.
Cancel button
The fundamental issue is that type 'button' still submits the form, resulting in extra processing. To stop the button from submitting the form you need to change the html that Drupal has rendered and change the button type to 'button'. This can be done in a #post_render function which has access to, and can modify the html that Drupal has generated (as well as the form element).
The attribute '#attributes' adds the required javascript to the button to make the page go back to the previous page, same as the back button would.
A solution I have come up with is this:
notes:
tested in Drupal 7 only
the #prefix attribute is used to put some space between the submit and cancel buttons.
WORKS in D&, too!
WORKS in D&, too!
Thanks a millon brother
U rock
Superb!
Cracking solution. Thank you.
Simpler solution without #post_render
event.preventDefault() will prevent submitting.
This met my need. Here's some details.
Here is the way I used it within a form_alter. Adding it to the ['actions'] array places it nicely next to the submit button.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
// Is this the form I want to edit?
if ($form_id == 'some_form' || $form_id == 'another_form') {
$form['actions']['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#weight' => -1,
'#attributes' => array('onClick' => 'history.go(-1); event.preventDefault();'),
);
}
}
Cancel button
Drupal 7 solution.
Work for me :))
Good luck
See https://drupal.org
See https://drupal.org/project/mb
Form Cancel Button=
Also see the Form Cancel Button sandbox project
Nice :-)
Haha, very cool.
Thx !
Best regards
Lars Jendrzejewski
Try the Node Form Settings module
The Node Form Settings module provides this along with a number of other options to control the buttons in a node and comment form.
https://www.drupal.org/project/nodeformsettings
It's available for D6 and D7.
I provide Drupal, Drupal Commerce and CiviCRM development services for customers in New Zealand and beyond
cancel_button
Use
https://www.drupal.org/project/cancel_button