Skip to content

Instantly share code, notes, and snippets.

@leepettijohn
Created September 25, 2017 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leepettijohn/4bc53924b7a328ffd1f829f69ad763bf to your computer and use it in GitHub Desktop.
Save leepettijohn/4bc53924b7a328ffd1f829f69ad763bf to your computer and use it in GitHub Desktop.
Gravity Forms - Limit checkbox choices after chosen
<?
// Change 'XXX' to your form id
$location_form_id = XXX;
add_filter( 'gform_pre_render_'.$location_form_id, 'limit_choices' );
add_filter( 'gform_pre_validation_'.$location_form_id, 'limit_choices' );
add_filter( 'gform_pre_submission_'.$location_form_id, 'limit_choices' );
add_filter( 'gform_pre_submission_filter_'.$location_form_id, 'limit_choices' );
add_filter( 'gform_admin_pre_render_'.$location_form_id, 'limit_choices' );
function limit_choices( $form ) {
global $wpdb;
$tablename = $wpdb->prefix . 'rg_lead_detail';
$jquerylimits = array();
foreach ( $form['fields'] as &$field ) {
if ( $field->id == xxx) {
foreach ($field->inputs as $eachchoice){
$sqlquery = "SELECT id FROM $tablename WHERE form_id=".$form['id']." AND FORMAT (field_number,3) = FORMAT(".$eachchoice['id'].",3) ORDER BY lead_id";
$values = $wpdb->get_var($sqlquery);
if (!empty($values)){
$jquerylimits[]='$(\'input[name="input_'.$eachchoice['id'].'"]\').attr("disabled","disabled");';
}
}
print_r('<script>jQuery(document).ready(function($){');
foreach ($jquerylimits as $eachlimit){
print_r($eachlimit);
}
print_r('});</script>');
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment