Skip Navigation

[Resolved] How to get values of checkboxes with php

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by simonV-3 1 year, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2580173

Tell us what you are trying to do? We are having a field (slug: feature-activities2) and like to check the value (used to be 1 or nothing) of the options we have there.

We have currently 10 options. I like then to create some php code and depending on the value to show something different.

Is there any documentation that you are following? https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes

and found this

types_render_field( "my-checkboxes", array( "separator" => ", " ) )

- but I am not sure where I do set what option to choose to check.

Is there a similar example that we can see? Actually I basically only have it with some short code from an old website:

<div class="feature-icons" style="text-align: center;margin-top: 20px;">
  
  	<a [wpv-conditional if="( [types field='feature-activities2' option='0'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Surfing"><img src="/wp-content/themes/Impreza-child/icons/006-surfer-surfing-white.png" width="64" height="64" class="aligncenter size-full" /></a>
  	
	<a [wpv-conditional if="( [types field='feature-activities2' option='1'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Sunbath"><img src="/wp-content/themes/Impreza-child/icons/002-sunbed-white.png" width="64" height="64" class="aligncenter size-full" /></a>
  
	<a [wpv-conditional if="( [types field='feature-activities2' option='2'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Swimming"><img src="/wp-content/themes/Impreza-child/icons/008-silhouette-white.png" width="64" height="64" class="aligncenter size-full" /></a>

	<a [wpv-conditional if="( [types field='feature-activities2' option='3'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Parachuting"><img src="/wp-content/themes/Impreza-child/icons/007-gliding-parachutist-white.png" width="64" height="64" class="aligncenter size-full" /></a>
  
  	<a [wpv-conditional if="( [types field='feature-activities2' option='4'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Snorkeling"><img src="/wp-content/themes/Impreza-child/icons/002-mask.png" width="64" height="64" class="aligncenter size-full" /></a>
</div>
<div class="feature-icons" style="text-align: center;">
  	<a [wpv-conditional if="( [types field='feature-activities2' option='5'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Pinnic"><img src="/wp-content/themes/Impreza-child/icons/004-shopping-basket-white.png" width="64" height="64" class="aligncenter size-full" /></a>

	<a [wpv-conditional if="( [types field='feature-activities2' option='6'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Restaurants"><img src="/wp-content/themes/Impreza-child/icons/003-dinner-white.png" width="64" height="64" class="aligncenter size-full" /></a>
  
  	<a [wpv-conditional if="( [types field='feature-activities2' option='7'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Bar"><img src="/wp-content/themes/Impreza-child/icons/001-drink.png" width="64" height="64" class="aligncenter size-full" /></a>
  
	<a [wpv-conditional if="( [types field='feature-activities2' option='8'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Beach shower"><img src="/wp-content/themes/Impreza-child/icons/002-shower-white.png" width="64" height="64" class="aligncenter size-full" /></a>

	<a [wpv-conditional if="( [types field='feature-activities2' option='9'][/types] eq '1' )"] class="active" [/wpv-conditional] title="Restroom"><img src="/wp-content/themes/Impreza-child/icons/001-toilet-white.png" width="64" height="64" class="aligncenter size-full" /></a>
</div>

But I like to do basically the same in php, due to the facts, that our theme isn't 100% compatible with conditional statements.

What is the link to your site?

#2581089

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

So you can use the same way the PHP field.

For example:

$option0 = types_render_field( "feature-activities2", array( "option" => "0" ) );
if($option0 == 1) {
  echo "Option 0" is checked";
}

$option1 = types_render_field( "feature-activities2", array( "option" => "1" ) );
if($option1 == 1) {
  echo "Option 1" is checked";
}

$option2 = types_render_field( "feature-activities2", array( "option" => "2" ) );
if($option2  == 1) {
  echo "Option 2" is checked";
}

You can adjust the above code for as many options you have.

#2581115

Dear Minesh

Great. If I like to have this a bit more dynamic, where I like to add an $atts - meaning the option 0, 1, 2 etc I will provide in the shortcode, how can I do that?

function display_beach_feature_surfing($atts) {
	global $post;
	$str = '';
	$surfing = types_render_field( "feature-activities2", array( "option" => "0" ) );
	if($surfing == 1) {
		$str .= '<a class="active" title="Surfing"><img src="/wp-content/themes/Impreza-child/icons/006-surfer-surfing-white.png" width="64" height="64" class="aligncenter size-full" /></a>';
	}
	else if($surfing != 1) {
		$str .= '<a class="active" title="Surfing"><img src="/wp-content/themes/Impreza-child/icons/006-surfer-surfing-white.png" width="64" height="64" class="aligncenter size-full" /></a>';
	}
	return $str;
}
add_shortcode( 'show_beach_feature_surfing', 'display_beach_feature_surfing' );
#2581123

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code as given under:

function display_beach_feature_surfing($atts) {
    global $post;
    $str = '';
    $surfing = types_render_field( "feature-activities2", array( "option" => $atts['option']) );
    if($surfing == 1) {
        $str .= '<a class="active" title="Surfing"><img src="/wp-content/themes/Impreza-child/icons/006-surfer-surfing-white.png" width="64" height="64" class="aligncenter size-full" /></a>';
    }
    else if($surfing != 1) {
        $str .= '<a class="active" title="Surfing"><img src="/wp-content/themes/Impreza-child/icons/006-surfer-surfing-white.png" width="64" height="64" class="aligncenter size-full" /></a>';
    }
    return $str;
}
add_shortcode( 'show_beach_feature_surfing', 'display_beach_feature_surfing' );

And you can call the above shortcode as:

[show_beach_feature_surfing option="0"]
[show_beach_feature_surfing option="1"]
#2581131

My issue is resolved now. Thank you!