Skip Navigation

[Résolu] Multiple Submit Buttons on CRED Form

This support ticket is created Il y a 4 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 4 années et 9 mois.

Assisté par: Beda.

Auteur
Publications
#1489203

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

#1489237

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?

#1489455

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');
		}       
	} 
}
#1491653

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

#1501641

Fabulous! Thank you for clarifying