I came across this post today https://toolset.com/forums/topic/second-submit-form-button-with-custom-redirect-to-page/#post-1162983
I have always understood that a form should only have ONE submit button so I was curious. I tested the solution in the above post and it DOES work but before I get too excited about the improvements this could make to user experience, are there situations where this would have a detrimental effect (CRED hooks for example or validation)?
Thanks
A form always can and should have only one submit button.
That's no different if you add 10 submit buttons to a form. Still, you can press only one. And that's the same as saying each form has one Submit Button.
If you want to add many of them, you can do so since ever, but the user can click only one. And, since each does the same, it is not any different than having one submit button.
Of course, with Christians custom code you can check on the $_POST name of the button pressed, and act accordingly, but this is unrelated to how many buttons there are on the form, as each will do only one thing: Submit the form.
Hooks and validation codes act on the Form ID, which is always the same
So, nothing can break there is you submit the form with some other way than the standard button.
Does that make sense?
Hi Beda
Thank you for clarifying.
In Christian's custom code, the redirect URL is determined by the button the User clicks on to submit the form. Taking this one stage further, can the same approach be used to apply different hooks? Something alone these lines for example??
add_action('cred_save_data', 'save_data_form_1977',10,2);
function save_data_form_1977($post_id, $form_data) {
if ($form_data['id']==1977) {
if (isset($_POST['form_submit_1'])) {
update_post_meta($post_id, 'wpcf-custom-field', 'Yes');
}
if (isset($_POST['form_submit_2'])) {
update_post_meta($post_id, 'wpcf-custom-field', 'No');
}
}
}
Sure, yes.
If you can check in the code what button is pressed, you can then use that "if" case for anything generally possible in the form.
Let's generalize this a bit more to understand.
You have a variable which can be either A or B (this is our $_POST field).
Now with PHP you differentiate the two cases and do either A or B depending on what the condition above returns.
Hence, it is perfectly possible to have 2 completely distinct workflows related to the time from pressing submit onwards
Fabulous! Thank you for clarifying