• Resolved cicini

    (@cicini)


    Sorry to bother you again, but I’m meeting a similar problem again.

    Is there any way to avoid this?

    • This topic was modified 3 years, 4 months ago by cicini.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Harmonic Design

    (@harmonic_design)

    Hi cicini,
    yes this can be changed as well and with a very similar method to what we did to the answers.

    If you edit ./hd-quiz/includes/functions.php on lines 530 and 531 you will see where the result content is printed. Keep the outer divs and remove the PHP inside <?php echo apply_filters('the_content', $pass_text); ?> and <?php echo apply_filters('the_content', $fail_text); ?>

    Great! The results content is no longer being printed.

    Now we need to grab the content and display it on your page once a quiz has been completed. In your case, it’s probably easier to edit the previous code I gave you. In the hdq_script.js file I gave you new code to update the submit function. Inside that code was the following.

    if (pass_percent >= HDQ.VARS.pass_percent) {
    	jQuery(".hdq_result_pass").show();
    } else {
    	jQuery(".hdq_result_fail").show();
    }

    We are going to edit that to also fetch what the result content should be. So change it to:

    if (pass_percent >= HDQ.VARS.pass_percent) {
    	jQuery.ajax({
    		type: "POST",
    		data: {
    			action: "hdq_get_result_text",
    			data: { quiz_ID: quiz_ID, status: "pass" },
    		},
    		url: HDQ.VARS.ajax,
    		success: async function (res) {
    			console.log(res);
    			jQuery(".hdq_result_pass").html(res);
    			jQuery(".hdq_result_pass").show();
    		},
    	});
    } else {
    	jQuery.ajax({
    		type: "POST",
    		data: {
    			action: "hdq_get_result_text",
    			data: { quiz_ID: quiz_ID, status: "fail" },
    		},
    		url: HDQ.VARS.ajax,
    		success: async function (res) {
    			console.log(res);
    			jQuery(".hdq_result_fail").html(res);
    			jQuery(".hdq_result_fail").show();
    		},
    	});
    }
    

    Now all that’s left is to add the function that actually prints out the needed text. You can add the following function to the end of index.php just like you did before.

    function hdq_get_result_text()
    {
    	$quiz_ID = intval($_POST["data"]["quiz_ID"]);
    	$status = sanitize_text_field($_POST["data"]["status"]);	
    	
    	$quiz_settings = get_hdq_quiz($quiz_ID);
        $pass_text = $quiz_settings["quiz_pass_text"]["value"];
        $fail_text = $quiz_settings["quiz_fail_text"]["value"];
    	
    	if($status === "pass"){
    		echo $pass_text;
    	} else {
    		echo $fail_text;
    	}
    	die();
    }
    add_action('wp_ajax_hdq_get_result_text', 'hdq_get_result_text');
    add_action('wp_ajax_nopriv_hdq_get_result_text', 'hdq_get_result_text');

    and that’s it!

    Thread Starter cicini

    (@cicini)

    It works! Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Users can get result(pass) from html.’ is closed to new replies.