如何自动提交表单

时间:2017-01-28 作者:crepuslily

我正在使用一个表单插件Formcraft。用户通过两个URL访问表单,每个URL在查询字符串中传递不同的参数。表单是使用条件逻辑设置的,因此每个URL显示不同的字段。

我想要的是一个选项,用户可以单击链接,自动填充字段,并发送数据。现在,这些字段会自动填充,但用户仍需单击提交按钮。

我想知道是否可以在表单php文件的某个地方添加一个if语句,以便在填写特定字段的情况下提交表单?或者,还有其他方法可以做到这一点吗?

任何帮助都将不胜感激。谢谢

我正在添加php文件的提交表单部分:

/*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 个回复
SO网友:Vishal Kumar Sahu

您可以使用javascript在几秒钟后自动提交表单。

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

setInterval(to_submit_form,3000);
像这样的。

相关推荐

Altered Media Library URLs

我有一个客户的网站,是在他们离开另一家代理机构后我找到的。该机构使用了一个专有主题和自己的自托管页面生成器,以防止其在除他们之外的任何其他托管环境中更新或编辑。它的另一个方面是重新映射主题的URL并上载目录。因此,例如,代替WP在中查找主题文件http://domain.com/wp-content/themes/…. 它在里面找他们http://domain.com/t/….同样,对于图像上载,也可以在http://domain.com/wp-content/uploads/…, 它在里面找他们http