Skip Navigation

[Resolved] Quiz form check answers before saving

This support ticket is created 3 years, 5 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: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by martinH-10 3 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#2098469

Tell us what you are trying to do?

- I created a CPT called QUIZ.
- I added custom fields as an answers. Radio buttons with values 1,2,3 where radio with the value 1 is the right answer.
- I created post form for users to fill the quiz.
Now I need to check their answers when they click to submit the form and every time the checked radio field is with value 1 add a value to the custom field called points.
So if user has 3 times checked radio field with value 1, then he gets 3 points. If he checked radio with ther value, he gets nothing.

How to do that with before_save_data?

I was thinking about:

result = 0;

if (radio1 = 1) {
result = +1;
} elseif (radio2=1) {
result=+1;
}

.... but dont know if it is possible somehow with Toolset Forms. My quiz is here: hidden link

Thanks for advice.

#2099031

Hello and thank you for contacting Toolset support.

I would suggest using the cred_save_data hook instead of the cred_before_save_data. It will add some overhead(1 read, and + 1 writes to the database). But I believe it to be the correct way to do it. Something like this:

add_action('cred_save_data','func_update_quiz_points',10,2);
function func_update_quiz_points($post_id,$form_data) {
	if ($form_data['id']==1121) {
        $answer = get_post_meta( $post_id, 'wpcf-answers', true);
        $result = 0;
	    if ( $result == 1 ) {
			$result += 1;
		} else {
		}
		update_post_meta( $post_id, 'wpcf-points', $result);
    }
}

The following code assumes that the custom fields slugs are 'answers' and 'points'.

However, I am not really sure to understand your use case. When you say So if user has 3 times checked radio field with value 1, then he gets 3 points. If he checked radio with their value, he gets nothing, would you mean the user has submitted the form 3 times? If that's the case, my suggested code won't be relevant, because each form submit will create a new post, and we will either save 1 or 0 for each post depending on the user's input.

Can you provide more details about your use case? What would you like to achieve? Can you describe it in small ideas, each on a single line? Or maybe provide a detailed example!

#2099289

Hello, it means, that I have multiple Radio Fields with same Answers.

Like:

Fieldname: Answer 1
Values: 1,2,3

Fieldname: Answer 2
Values: 1,2,3

Fieldname: Answer 3
Values: 1,2,3

Fieldname: Answer 4
Values: 1,2,3

Fieldname: Answer 5
Values: 1,2,3

I need to check which opinion user checked. If it is Value 1, then do the result +1.

Also, I would like to enable user to fill the form only once.

#2099341

Awesome. So, assuming that the first option for each radio field is the correct answer(should increment result), and that the fields slugs(instead of names) are answer-1, answer-2, answer-3, answer-4, and answer-5. And that the custom field's slug to store the result is 'points'. The custom code would be:

add_action('cred_save_data','func_update_quiz_points',10,2);
function func_update_quiz_points($post_id,$form_data) {
    // run this code only for the form with ID #1234
    if ($form_data['id']==1234) {
        $answer1 = get_post_meta( $post_id, 'wpcf-answer-1', true);
        $answer2 = get_post_meta( $post_id, 'wpcf-answer-2', true);
        $answer3 = get_post_meta( $post_id, 'wpcf-answer-3', true);
        $answer4 = get_post_meta( $post_id, 'wpcf-answer-4', true);
        $answer5 = get_post_meta( $post_id, 'wpcf-answer-5', true);
        $result = 0;
        if ( $answer1 == 1 ) {
            $result += 1;
        }
        if ( $answer2 ​== 1 ) {
           ​$result += 1;
       ​}
       ​if ( $answer3 == 1 ) {
           ​$result += 1;
       ​}
       ​if ( $answer4 == 1 ) {
           ​$result += 1;
       ​}
       ​if ( $answer5 == 1 ) {
           ​$result += 1;
       ​}
       ​update_post_meta( $post_id, 'wpcf-points', $result);
   ​}
}

Please note that we prefix the Toolset defined fields with 'wpcf-'. A field with slug 'points' will be stored on the database with the meta_key 'wpcf-points'.

Regarding the possibility to let the user answer only one time, there are a couple of ways to do it. Using custom code or using a view. Please check these previous cases from the forum hidden link
However, for support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I have to kindly ask you to open a new thread if you need further assistance with this different request. And let us focus, on this ticket, on your previous request.

#2099541

Awesome man!