Skip Navigation

[Resolved] Conditional select field.

This support ticket is created 2 years, 9 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 8 replies, has 3 voices.

Last updated by matthiasV-2 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2121033

Pass this ticket to Waqar, *unless you can offer a valuable solution step-by-step before Waqar comes back from vacation this friday*.

Hi Waqar,

On this page hidden link we have a 'select field' 'Ik wil het aanbod gratis bekijken'.

Now, we want 3 things:

1. We want to redirect users to a different page after submitting based on whether or not the field is selected.

2. We want to send a different notification email after submitting based on whether or not the field is selected.

For example 1 and 2:

If field 'Ik wil het aanbod gratis bekijken' is selected, the submit button goes to page A and notification A is sent to the user & admin.
If field 'Ik wil het aanbod gratis bekijken' is not selected, the submit button goes to page B and notification B is sent to the user & admin.

3. In the form we have a conditional with below fields that are required:
'Ik heb'
'straatnaam'
'huisnummer'
'woonplaats'
We want the above required fields to be 'mandatory' if the field 'Ik wil het aanbod gratis bekijken' is selected.
If the field 'Ik wil het aanbod gratis bekijken' is not selected the required fields should 'not' be mandatory.

#2121843

As your request, I have passed this ticket to Waqar

#2123411

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for waiting and I'd be happy to assist.

For the 3 points that you've shared, there is no built-in option available, which is why some custom code will be needed.

Some tests with a similar form and settings will be needed on a test website, to suggest the most efficient ways to achieve these.

I'll complete these tests over the weekend and will be able to share my findings by Monday.

Thank you for your patience.

regards,
Waqar

#2125499

Hi Waqar,

Can you share your solution please?

#2126539

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting.

1. We want to redirect users to a different page after submitting based on whether or not the field is selected.

- To programmatically redirect the visitors to a different page, when the form is submitted, based on the "Ik wil het aanbod gratis bekijken" checkbox, you can use the filter "cred_success_redirect":
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

For example, the ID of the user form in question is "18888" and suppose that if that field is checked, you'd like to redirect to the page with ID "123" and if unchecked, redirect to the page with ID "456". The code in this case would look like this:


add_filter('cred_success_redirect', 'custom_redirect_user_form_18888',10,3);
function custom_redirect_user_form_18888($url, $post_id, $form_data)
{
	// process only if specific form
	if ($form_data['id']==18888) {
		// ID of the page to redirect to when the field is checked
		$checked_paged_id = 123;
		// ID of the page to redirect to when the field is not checked
		$unchecked_paged_id = 456;
		// check if the field is checked
		if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
			return get_post_permalink( $checked_paged_id );
		}
		else
		{
			return get_post_permalink( $unchecked_paged_id );
		}
	}
	return $url;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

For the other two questions, I've created separate tickets and will reply to them shortly.

#2126973
after visitors submit form.png
page after visitors submit form.png

Hi Waqar,

We have setup the below custom code called "custom registration confirmation", HOWEVER we are not getting redirected accordingly to:

1. hidden link IF user did NOT check "Ik wil het aanbod gratis bekijken".
OR
2. hidden link IF user did check "Ik wil het aanbod gratis bekijken".

Instead users stay on the form page (see attachment).
For the form we applied: Go to "Bedankt om in te schrijven!" (see attachment). We can only add 1 page here. I don't think this makes sense as we want to redirect users based on the checkbox.

What is wrong here?
Please test everything from your side.

add_filter('cred_success_redirect', 'custom_redirect_user_form_18888',10,3);
function custom_redirect_user_form_18888($url, $post_id, $form_data)
{
// process only if specific form
if ($form_data['id']==18888) {
// ID of the page to redirect to when the field is checked
$checked_paged_id = 19006;
// ID of the page to redirect to when the field is not checked
$unchecked_paged_id = 672;
// check if the field is checked
if(isset($_POST['wpcf-ik-wil-het-aanbod-gratis-bekijken'][0])) {
return get_post_permalink( $checked_paged_id );
}
else
{
return get_post_permalink( $unchecked_paged_id );
}
}
return $url;
}

#2128607

Waqar

Can we solve these issues now?

#2128875

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting and I apologize for the delay in getting back to this.

For the "cred_success_redirect" hook to work, it is important that the form must be set to redirect to a specific page or a post after the submission.
( there is a note added about this in our documentation as well: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect )

When I checked your form "Nieuwe gebruiker registreren DEFINITIEF" earlier, it was set to redirect to a page after submission.

But, now that setting was changed and the form was set to keep displaying, which is why the custom code attached to the "cred_success_redirect" hook was not working. I've changed that setting to redirect to a page and the redirection code is working now.

Note: It doesn't matter which page is selected for redirection in the form's settings, as the actual page for redirection will be selected by the custom code.

#2132505

My issue is resolved now. Thank you Waqar!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.