-1

I am using a form plugin, Formcraft. Users get to the form via two URLs, each passing different parameters in the query string. The form is set up with conditional logic so each URL shows different fields.

What I want is an option where a user can click a link, fields are automatically populated, AND the data is sent. Right now the fields are automatically populated but the user still has to click the SUBMIT button.

I was wondering if I could add an IF statement somewhere in the form php file to submit the form under the condition that a particular field is filled in? OR, is there another way to do this?

Any help would be greatly appreciated. Thanks.

I'm adding the submit form portion of the php file:

/*Submit The Form*/
add_action('wp_ajax_formcraft3_form_submit', 'formcraft3_form_submit');
add_action('wp_ajax_nopriv_formcraft3_form_submit',   'formcraft3_form_submit');
function formcraft3_form_submit()
{
global $fc_meta, $fc_forms_table, $fc_submissions_table, $fc_files_table, $wpdb, $fc_final_response;
if ( !isset($_POST['id']) || !ctype_digit($_POST['id']) )
{
  echo json_encode(array('failed'=> __('Invalid Form ID','formcraft') ));
  die();
}
if ( isset($_POST['website']) && $_POST['website']!='' )
{
  echo json_encode(array('failed'=> __('SPAM detected','formcraft') ));
  die();
}
$id = $_POST['id'];
$meta = $wpdb->get_var( "SELECT meta_builder FROM $fc_forms_table WHERE id=$id" );
$meta = json_decode(stripcslashes($meta), 1);

/* Allow Editing of Meta */
$meta = apply_filters('formcraft_filter_entry_meta', $meta);

$fc_final_response = array();
$fc_final_response['errors'] = array();

$_POST = apply_filters( 'formcraft_filter_raw', $_POST, $meta );

$integrations = array();
$integrations['not_triggered'] = array();
$_POST['trigger_integration'] = isset($_POST['triggerIntegration']) ? $_POST['triggerIntegration'] : $_POST['trigger_integration'];     
$_POST['trigger_integration'] = isset($_POST['trigger_integration']) ? $_POST['trigger_integration'] : '';
$integrations['triggered'] = json_decode(stripcslashes(urldecode($_POST['trigger_integration'])), 1);
$integrations['triggered'] = count($integrations['triggered']) > 0 ? array_unique($integrations['triggered']) : $integrations['triggered'];
if ( isset($meta['config']['Logic']) )
{
  foreach ($meta['config']['Logic'] as $key => $logicRow) {
    if ( isset($logicRow[1]) && is_array($logicRow[1]) && count($logicRow[1])>0 )
    {
      foreach ($logicRow[1] as $key2 => $value) {
        if ( isset($value[0]) && isset($value[3]) && $value[0]=='trigger_integration' && !in_array($value[3], $integrations['triggered']) )
        {
          $integrations['not_triggered'][] = $value[3];
        }
      }
    }
  }
}
$messages = $meta['config']['Messages'];
$hidden_fields = isset($_POST['hidden']) ? explode(',', preg_replace('/\s+/', '', $_POST['hidden'])) : array();
foreach ($meta['fields'] as $key => $field) {

  if ( isset($_POST['type']) && ctype_digit($_POST['type']) && $field['page']!=$_POST['type'] ){continue;}

  $value = isset($_POST[$field['identifier']]) ? $_POST[$field['identifier']] : '';

  if ( !in_array($field['identifier'], $hidden_fields) )
  {
    /* Check if Required Field */
    if ($field['type']=='matrix' && isset($field['elementDefaults']['required']) && $field['elementDefaults']['required']==true)
    {
      foreach ($field['elementDefaults']['matrix_rows_output'] as $matrix_key => $matrix_value) {
        if ( !isset($_POST[$field['identifier'].'_'.$matrix_key]) )
        {
          $fc_final_response['errors'][$field['identifier']] = $messages['is_required'];
          break;
        }
      }         
    }
    else if ( isset($field['elementDefaults']['required']) && is_array($value) && $field['elementDefaults']['required']==true && (count($value)==0 || $value[0]=='' ) )
    {
      $fc_final_response['errors'][$field['identifier']] = $messages['is_required'];
    }
    else if ( isset($field['elementDefaults']['required']) && $field['elementDefaults']['required']==true && !is_array($value) && trim($value)==''  )
    {
      $fc_final_response['errors'][$field['identifier']] = $messages['is_required'];
    }
    else if (isset($field['elementDefaults']['required']) && $field['elementDefaults']['required']==true && !isset($_POST[$field['identifier']]))
    {
      $fc_final_response['errors'][$field['identifier']] = $messages['is_required'];
    }

    /* Field Type Validation */
    switch ($field['type']) {
      case 'email':
      if ( trim($value)!='' && filter_var( $value, FILTER_VALIDATE_EMAIL ) == false )
      {
        $fc_final_response['errors'][$field['identifier']] = $messages['allow_email'];
      }
      break;

      case 'fileupload':
      if ( isset($field['elementDefaults']['min_files']) && ctype_digit($field['elementDefaults']['min_files']) && $field['elementDefaults']['min_files']!=0 )
      {
        if (!isset($_POST[$field['identifier']]))
        {
          $fc_final_response['errors'][$field['identifier']] = str_ireplace('[x]', $field['elementDefaults']['min_files'], $messages['min_files']);
        }
        else if ( count($_POST[$field['identifier']]) < $field['elementDefaults']['min_files'] )
        {
          $fc_final_response['errors'][$field['identifier']] = str_ireplace('[x]', $field['elementDefaults']['min_files'], $messages['min_files']);
        }
      }
      break;

      default:
      break;
    }

    /* Explicit Validation */
    if ( isset($field['elementDefaults']) && isset($field['elementDefaults']['Validation']) )
    {
      $spaces = isset($field['elementDefaults']['Validation']['spaces']) && $field['elementDefaults']['Validation']['spaces']==true ? true : false;
      $value_to_check = $spaces==true ? str_replace(' ', '', $value) : $value;
      $value = is_array($value) ? $value[0] : $value;
      foreach ($field['elementDefaults']['Validation'] as $type => $validation) {
        if (empty($value)){continue;}
        switch ($type) {
          case 'allowed':
          $value_to_check = is_array($value_to_check) ? $value_to_check[0] : $value_to_check;
          if ( $validation=='alphabets' && !ctype_alpha($value_to_check) )
          {
            $fc_final_response['errors'][$field['identifier']] = $messages['allow_alphabets'];
          }
          else if ( $validation=='numbers' && !ctype_digit($value_to_check) )
          {
            $fc_final_response['errors'][$field['identifier']] = $messages['allow_numbers'];
          }
          else if ( $validation=='alphanumeric' && !ctype_alnum($value_to_check) )
          {
            $fc_final_response['errors'][$field['identifier']] = $messages['allow_alphanumeric'];
          }
          else if ( $validation=='url' && !filter_var( $value, FILTER_VALIDATE_URL ) )
          {
            $fc_final_response['errors'][$field['identifier']] = $messages['allow_url'];
          }
          break;

          case 'minChar':
          if ( !ctype_digit($validation) ) break;
          if ( (mb_strlen($value)-substr_count( $value, "\n" )) < $validation )
          {
            $fc_final_response['errors'][$field['identifier']] = str_ireplace('[x]', $validation, $messages['min_char']);
          }
          break;

          case 'maxChar':
          if ( !ctype_digit($validation) ) break;
          if ( (mb_strlen($value)-substr_count( $value, "\n" )) > $validation )
          {
            $fc_final_response['errors'][$field['identifier']] = str_ireplace('[x]', $validation, $messages['max_char']);
          }
          break;

          default:
          break;
        }
      }
    }
  }

} /* End of Fields Loop */


/* If validation failed, show errors */
if ( count($fc_final_response['errors'])>0 )
{
  if ( !isset($fc_final_response['failed']) )
  {
    $fc_final_response['failed'] = isset($meta['config']['messages']['form_errors']) ? $meta['config']['messages']['form_errors'] : $messages['failed'];
  }
  echo json_encode($fc_final_response);
  die();
}
if ( !isset($_POST['type']) || $_POST['type']!='all' )
{
  echo json_encode(array('validated'=>$_POST['type']));
  die();
}
/* ELSE All is Well with the Submission */
-1

You can use javascript to automatically submit the form after few seconds.

function to_submit_form(){
    document.getElementById("myForm").submit();
}

setInterval(to_submit_form,3000);

Something like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.