• Hi,

    My client like to use this plugin and I love it, but now he need to create a form where depending if a checkbox is marked or a select option is selected shows or hide some other fields, I read on other Topics is possible using JS or CSS, but in this case my client what to create the forms by they own, changed and make new ones, if I have to code a JS every time he create a form I will be crazy any time.

    So how can I do it?, is an easy way to?

    Thanx for any help.

    http://wordpress.org/plugins/contact-form-7/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Looks like you are looking for Conditional Logic which is not available directly in the Contact Form 7 plugin. It can be done via custom programing (with PHP, JS or CSS). Some of the premium Form plugins do offer Conditional Logic including Gravity Forms, Quform and Ninja Forms.

    Thread Starter Pancho Perez

    (@lonchbox)

    Hi @buzztone actually I found a way, using a JS and creating new cform7 new shotcodes.

    First I create a shortcode to display some specific CPTs.

    wpcf7_add_shortcode('carreras_modalidades', 'createbox_modalidades', true);
    function createbox_modalidades($tag){
    
    	$name = 'carreras_modalidades';
    	$options = (array) $tag['options'];
    	$values = (array) $tag['values'];
    
    	foreach ($options as $option) {
    		if (preg_match('%^(\d*)[/x](\d*)$%i', $option, $matches)) {
    			$size_att = (int) $matches[1];
    			$maxlen_att = (int) $matches[2];
    		}
    	}
    
    	$output = "<select name='".$name."' id='".$name."' onchange='document.getElementById(\"" . $name ."\").value=this.value;' style='min-width:".$maxlen_att."px; max-width:".$maxlen_att."px; border:none; margin-top: 5px; margin-bottom:5px;'><option>Escoja una modalidad</option>";
    
    	$terms = get_terms("modalidad");
    
    	$count = count($terms);
    
    	if ( $count > 0 ){
    		foreach ( $terms as $term ) {
    
    			$output .= "<option value='".$term->slug."'>".$term->name . "</option>";
    
    		}
    	}
    
    	$output .= "</select>";
    
    	return $output;
    }

    then using markup inside the form editor to display this new selector shortcode and the two spaces to hide and show other custom selectors.

    [carreras_modalidades]
    
    <div id="show1" style="display:none;">
    [carreras_diurna]
    </div>
    <div id="show2" style="display:none;">
    [carreras_nocturna]
    </div>

    Tho show and hide need a custom JS:

    /* wpcof7 hide/show animation */
    	$("select[name='carreras_modalidades']").change(function() {
    		if ($( this ).val() == 'diurna') {
    			$("div#show1").show("slow");
    			$("div#show2").hide("slow");
    		else if ($( this ).val() == 'nocturna') {
    			$("div#show1").hide("slow");
    			$("div#show2").show("slow");
    		}
    	});

    Ok, this works. But the problem is in the email confirmation.
    If I include the shortcodes in the email it works but only show the value of the ones selected, this means the hiden ones are printed as shortcode. ex:

    De: [your-name] <[your-email]>
    Asunto: [your-subject]
    
    [your-message]
    
    Modalidades:
    [carreras_modalidades]
    -
    [carreras_diurna] [carreras_nocturna]

    If the user select diurna and send the form the email show the [carreras_diurna] values but the [carreras_nocturna] obviously is not selected so the email show the shortcode as a text, how can I fix this?

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

The topic ‘Hide/show fields without JS or CSS’ is closed to new replies.