如何将数据从表单%1移动到表单%2

时间:2017-06-07 作者:DigitalDesigner

我有一个两步网站注册我的WordPress网站。

BEGIN EDIT

第一步/表单是我构建的前端验证表单插件,它检查一个自定义\\u表,以验证访问者是否是自定义\\u表的一部分。

第二个步骤/表单是我构建的前端注册表插件,我在其中3个字段上使用了与第一个表单相同的“名称”,并在第二个表单输入字段的值中回显了第一个表单输入字段。

END EDIT

我想做的是让第一个表单中3个字段的表单数据填充第二个表单中的3个字段。

我尝试将第二个url添加到表单的操作区域,并匹配表单的名称,该表单确实成功地将数据填充到了第二个表单,但在执行此操作时,我的验证停止了对第一个表单的操作。因此,我放弃了这个想法。

我现在还编辑了代码,以反映我对hwl下面的评论所做的更改,并希望进一步了解,因为现在我看到验证脚本正在工作,但当您输入成功的信息时,表单仍然没有在第二页加载信息。但是也没有致命的错误,所以这是一件好事。

BEGIN EDIT - DISPLAY BOTH FULL FORMS

FORM 1 - Verification Plugin

 <?php
 /*
  * Plugin Name: Custom Member Verification
  * Plugin URI: http://digitaldesigneronline.com
  * Description: Adds a Custom Member Verification Form to your WordPress website with a simple shortcode. Use the shortcode [cv] to add the verification form to a page, post or widget area.
  * Version: 1.0
  * Author: Chad Warford - The Online Lifesaver
  * Author URI: http://digitaldesigneronline.com
  */

 function cv(&$fields, &$errors) {

   // Check args and replace if necessary
   if (!is_array($fields))     $fields = array();
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Check for form submit
   if (isset($_POST[\'submit\'])) {

     // Get fields from submitted form
     $fields = cv_get_fields();

     // Validate fields and produce errors
     if (cv_validate($fields, $errors)) {

    // If successful, display a message

      // Clear field data
      // $fields = array(); 
     }
   }

   // Santitize fields
   cv_sanitize($fields);

   // Generate form
   cv_display_form($fields, $errors);
 }

 function cv_sanitize(&$fields) {
   $fields[\'user_login\']   =  isset($fields[\'user_login\'])  ? sanitize_user($fields[\'user_login\']) : \'\';
   $fields[\'first_name\']   =  isset($fields[\'first_name\'])  ? sanitize_text_field($fields[\'first_name\']) : \'\';
   $fields[\'last_name\']    =  isset($fields[\'last_name\'])   ? sanitize_text_field($fields[\'last_name\']) : \'\';
   $fields[\'SinLast4\']    =  isset($fields[\'SinLast4\'])   ? esc_attr($fields[\'SinLast4\']) : \'\';
 }

 function cv_display_form($fields = array(), $errors = null) {

   // Check for wp error obj and see if it has any errors  
   if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {

     // Display errors
     ?>
    <div style="display:block;margin:auto;text-align:left;max-width:1080px;">
    <ul><?php
     foreach ($errors->get_error_messages() as $key => $val) {
       ?><li>
         <?php echo $val; ?>
       </li><?php
     }
     ?></ul><?php
   }

   // Display form
   ?>
   </div>  
   <div style="display:block;margin:auto;text-align:left;max-width:1080px;">

   <form action="<?php $_SERVER[\'REQUEST_URI\'] ?>" method="post">
     <div>
        <label for="user_login">Card Number <strong>*</strong></label>
        <input type="text" name="user_login" id="user_login" value="<?php echo (isset($fields[\'user_login\']) ? $fields[\'user_login\'] : \'\') ?>" >
     </div>
    <br>
    <div>
       <label for="first_name">First Name <strong>*</strong></label>
       <input type="text" name="first_name" id="first_name" value="<?php echo (isset($fields[\'first_name\']) ? $fields[\'first_name\'] : \'\') ?>" >
     </div>
    <br>
     <div>
       <label for="last_name">Last Name <strong>*</strong></label>
       <input type="text" name="last_name" id="last_name" value="<?php echo (isset($fields[\'last_name\']) ? $fields[\'last_name\'] : \'\') ?>" >
     </div>
    <br>
     <div>
       <label for="SinLast4">Last 4 digits of SIN:<strong>*</strong></label>
       <input type="text" name="SinLast4" id="SinLast4" value="<?php echo (isset($fields[\'SinLast4\']) ? $fields[\'SinLast4\'] : \'\') ?>">
     </div>
    <br>
     <br>
     <input type="submit" name="submit" value="Verify Membership">
     </form>
    <form action="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" method="post">
        <input type="hidden" name="user_login" value="<?php echo $_POST[\'user_login\']; ?>">
        <input type="hidden" name="first_name" value="<?php echo $_POST[\'first_name\']; ?>">
        <input type="hidden" name="last_name" value="<?php echo $_POST[\'last_name\']; ?>">
    </form>
 </div>
 <?php
 }

 function cv_get_fields() {
   return array(
     \'user_login\'   =>  isset($_POST[\'user_login\'])   ?  $_POST[\'user_login\']   :  \'\',
     \'first_name\'   =>  isset($_POST[\'first_name\'])   ?  $_POST[\'first_name\']        :  \'\',
     \'last_name\'    =>  isset($_POST[\'last_name\'])    ?  $_POST[\'last_name\']        :  \'\',
     \'SinLast4\'   =>  isset($_POST[\'SinLast4\'])   ?  $_POST[\'SinLast4\']        :  \'\',
   );
 }

 function cv_validate(&$fields, &$errors) {

   // Make sure there is a proper wp error obj
   // If not, make one
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Validate form data

   if (empty($fields[\'user_login\']) || empty($fields[\'first_name\']) || empty($fields[\'last_name\']) || empty($fields[\'SinLast4\'])) {
     $errors->add(\'field\', \'Please remember to enter your information exactly as it appears on your Dues Receipt.\');
   }

   if (strlen($fields[\'user_login\']) < 7) {
   $errors->add(\'username_length\', \'Card Number too short. Pleas verify you have entered the entire Card Number\');
   }

     if (strlen($fields[\'SinLast4\']) < 4) {
   $errors->add(\'sin_length\', \'Please verify you have entered only the last 4 digits of your SIN.\');
   }
   // If errors were produced, fail
   if (count($errors->get_error_messages()) > 0) {
     return false;
   }

   // Define $Card $First $Last $SinLast4
        global $wpdb;
        $Card=$_POST[\'user_login\'];
        $First=$_POST[\'first_name\'];
        $Last=$_POST[\'last_name\'];
        $SinLast4=$_POST[\'SinLast4\'];
           $result = $wpdb->get_results( "SELECT * FROM custom_table WHERE Card = \'$Card\' AND First = \'$First\' AND Last = \'$Last\' AND SinLast4 = \'$SinLast4\'");
          // $query = "SELECT * FROM custom_table WHERE Card = $Card AND First = $First AND Last = $Last AND SinLast4 = $SinLast4";
          // $result = $wpdb->get_results($query);
        foreach ( $result as $query )   {
        echo "<script>window.location=\'http://localhost/dev1/member-registration/\';</script>";  // Update URL to registration page
        header("location: http://localhost/dev1/member-registration/"); // Redirecting To Other Page
        } 
   // Else, success!
   return true;
 }


 // The callback function for the [cv] shortcode
 function cv_cb() {
   $fields = array();
   $errors = new WP_Error();

   // Buffer output
   ob_start();

   // Custom verification, go!
   cv($fields, $errors);

   // Return buffer
   return ob_get_clean();
 }
 add_shortcode(\'cv\', \'cv_cb\');

FORM 2 - Registration Plugin

 <?php

 /*
  * Plugin Name: Custom Registration
  * Description: Adds a Custom Registration Form to your WordPress website with a simple shortcode. Use the shortcode [cr] to add the registration form to a page, post or widget area.
  * Version: 1.0
  */

 function cr(&$fields, &$errors) {

   // Check args and replace if necessary
   if (!is_array($fields))     $fields = array();
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Check for form submit
   if (isset($_POST[\'submit\'])) {

     // Get fields from submitted form
     $fields = cr_get_fields();

     // Validate fields and produce errors
     if (cr_validate($fields, $errors)) {

    // If successful, register user
       $user_id = wp_insert_user($fields);

      // update user metadata
       update_user_meta( $user_id, \'wpcf-user-join-date\', time()); /////insert account creation date
       // Send the new user the welcome email
       wp_new_user_notification($user_id,$fields[\'user_pass\']);

       // And display a message
       echo nl2br ("Your account has been created and an activation link has been sent to the email address you entered.\\n Note that you must activate the account by clicking on the activation link when you get the email before you can login.");

       // Clear field data
       $fields = array(); 
     }
   }

   // Santitize fields
   cr_sanitize($fields);

   // Generate form
   cr_display_form($fields, $errors);
 }

 function cr_sanitize(&$fields) {
   $fields[\'user_login\']   =  isset($fields[\'user_login\'])  ? sanitize_user($fields[\'user_login\']) : \'\';
   $fields[\'first_name\']   =  isset($fields[\'first_name\'])  ? sanitize_text_field($fields[\'first_name\']) : \'\';
   $fields[\'last_name\']    =  isset($fields[\'last_name\'])   ? sanitize_text_field($fields[\'last_name\']) : \'\';
   $fields[\'user_email\']   =  isset($fields[\'user_email\'])  ? sanitize_email($fields[\'user_email\']) : \'\';
   $fields[\'email_confirm\']   =  isset($fields[\'email_confirm\'])  ? sanitize_email($fields[\'email_confirm\']) : \'\';
   $fields[\'user_pass\']    =  isset($fields[\'user_pass\'])   ? esc_attr($fields[\'user_pass\']) : \'\';
   $fields[\'user_pass_retyped\']    =  isset($fields[\'user_pass_retyped\'])   ? esc_attr($fields[\'user_pass_retyped\']) : \'\';
 }

 function cr_display_form($fields = array(), $errors = null) {

   // Check for wp error obj and see if it has any errors  
   if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {

     // Display errors
     ?>
    <div style="display:block;margin:auto;text-align:left;max-width:1080px;">
    <ul><?php
     foreach ($errors->get_error_messages() as $key => $val) {
       ?><li>
         <?php echo $val; ?>
       </li><?php
     }
     ?></ul><?php
   }

   // Display form
   ?>
   </div>
   <?php wp_enqueue_script( \'password-strength-meter\' ); ?>

   <div style="display:block;margin:auto;text-align:left;max-width:1080px;">

   <form action="<?php $_SERVER[\'REQUEST_URI\'] ?>" method="post">
     <div>
        <label for="user_login">Card Number <strong>*</strong></label>
        <input type="text" name="user_login" value="<?php echo $_POST[\'user_login\']; ?><?php echo (isset($fields[\'user_login\']) ? $fields[\'user_login\'] : \'\') ?>" >
     </div>
    <br>
    <div>
       <label for="first_name">First Name <strong>*</strong></label>
       <input type="text" name="first_name" value="<?php echo $_POST[\'first_name\']; ?><?php echo (isset($fields[\'first_name\']) ? $fields[\'first_name\'] : \'\') ?>" >
     </div>
    <br>
     <div>
       <label for="last_name">Last Name <strong>*</strong></label>
       <input type="text" name="last_name" value="<?php echo $_POST[\'last_name\']; ?><?php echo (isset($fields[\'last_name\']) ? $fields[\'last_name\'] : \'\') ?>" >
     </div>
    <br>
     <div>
       <label for="email">Email <strong>*</strong></label>
       <input type="text" name="user_email" value="<?php echo (isset($fields[\'user_email\']) ? $fields[\'user_email\'] : \'\') ?>">
     </div>
    <br>
     <div>
       <label for="confirm-email">Confirm Email <strong>*</strong></label>
       <input type="text" name="email_confirm" value="<?php echo (isset($fields[\'email_confirm\']) ? $fields[\'email_confirm\'] : \'\') ?>">
     </div>
    <br>
     <div>
       <label for="user_pass">Password <strong>*</strong></label>
       <input type="password" name="user_pass">
    </div>
    <div>
      <label for="user_pass_retyped">Re-Enter Password <strong>*</strong></label>
      <input type="password" name="user_pass_retyped">
    </div>
    <div>
      <span id="password-strength"></span>
     </div>
     <br>
     <input type="submit" name="submit" value="Register">
     </form>
    </div>
 <?php
 }

 function cr_get_fields() {
   return array(
     \'user_login\'   =>  isset($_POST[\'user_login\'])   ?  $_POST[\'user_login\']   :  \'\',
     \'first_name\'   =>  isset($_POST[\'first_name\'])   ?  $_POST[\'first_name\']        :  \'\',
     \'last_name\'    =>  isset($_POST[\'last_name\'])    ?  $_POST[\'last_name\']        :  \'\',
     \'user_email\'   =>  isset($_POST[\'user_email\'])   ?  $_POST[\'user_email\']        :  \'\',
     \'email_confirm\'   =>  isset($_POST[\'email_confirm\'])   ?  $_POST[\'email_confirm\']        :  \'\',
     \'user_pass\'    =>  isset($_POST[\'user_pass\'])    ?  $_POST[\'user_pass\']    :  \'\',
     \'user_pass_retyped\'    =>  isset($_POST[\'user_pass_retyped\'])    ?  $_POST[\'user_pass_retyped\']    :  \'\'
   );
 }

 function cr_validate(&$fields, &$errors) {

   // Make sure there is a proper wp error obj
   // If not, make one
   if (!is_wp_error($errors))  $errors = new WP_Error;

   // Validate form data

   if (empty($fields[\'user_login\']) || empty($fields[\'first_name\']) || empty($fields[\'last_name\']) || empty($fields[\'user_email\']) || empty($fields[\'email_confirm\']) || empty($fields[\'user_pass\']) || empty($fields[\'user_pass_retyped\'])) {
     $errors->add(\'field\', \'Please complete the registration form.\');
   }

   if (($fields[\'user_pass\'].value) != ($fields[\'user_pass_retyped\'].value))
     {
    $errors->add(\'user_pass_retyped\', \'Those passwords don\\\'t match!\');
     }

   // if (strlen($fields[\'user_login\']) < 8) {
   //  $errors->add(\'username_length\', \'Card Number too short. Pleas verify you have entered the entire Card Number\');
   // }

   if (username_exists($fields[\'user_login\']))
     $errors->add(\'user_name\', \'Sorry, that Card Number has already been registered.\');

   if (strlen($fields[\'user_pass\']) < 8) {
     $errors->add(\'user_pass\', \'Password length must be greater than 8\');
   }

   if (!is_email($fields[\'user_email\'])) {
     $errors->add(\'email_invalid\', \'Please verify the email address prior to submitting.\');
   }

   if (!is_email($fields[\'email_confirm\'])) {
     $errors->add(\'email_confirm_invalid\', \'Please verify the email address prior to submitting.\');
   }

   if (($fields[\'user_email\'].value) != ($fields[\'email_confirm\'].value))
     {
     $errors->add(\'email_mismatch\', \'Those emails don\\\'t match!\');
     }

   if (email_exists($fields[\'user_email\'])) {
     $errors->add(\'email_taken\', \'Please use another email address as it appears that one is already in use.\');
   }

   // If errors were produced, fail
   if (count($errors->get_error_messages()) > 0) {
     return false;
   }

   // Else, success!
   return true;
 }

 // Redefine user notification function
 if ( !function_exists(\'wp_new_user_notification\') ) {
     function wp_new_user_notification( $user_id, $plaintext_pass = \'\' ) {
        $user = new WP_User($user_id);
        $hash = md5( $random_number );
        add_user_meta( $user_id, \'hash\', $hash );
        $user_login = stripslashes($user->user_login);
        $first_name = stripslashes($user->first_name);
        $user_email = stripslashes($user->user_email);

         $message  = sprintf(__(\'New user registration on your blog %s:\'), get_option(\'blogname\')) . "\\n";
         $message .= sprintf(__(\'Username: %s\'), $user_login) . "\\n";
         $message .= sprintf(__(\'First Name: %s\'), $first_name) . "\\n";
         $message .= sprintf(__(\'E-mail: %s\'), $user_email) . "\\n";

         @wp_mail(get_option(\'admin_email\'), sprintf(__(\'[%s] New User Registration\'), get_option(\'blogname\')), $message);

         if ( empty($plaintext_pass) )
             return;

         $message  = sprintf(__(\'Hello %s,\'), $first_name) . "\\n";
         $message .= sprintf(__("Thank you for registering at %s."), get_option(\'blogname\')) . "\\n";
      // $message .= wp_login_url() . "\\n";
         $message .= sprintf(__(\'Your account is created and must be activated before you can use it.\')) . "\\n";
        $message .= sprintf(__(\'To activate the account click on the following link or copy-paste it in your browser:\')) . "\\n";
        $message .= home_url(\'/\').\'activate?id=\'.$un.\'&key=\'.$hash."\\n";
        $message .= sprintf(__(\'After activation you may login to %s\'), site_url()) . "\\n";
         $message .= sprintf(__(\'Here are your Login Credentials:\')) . "\\n";
         $message .= sprintf(__(\'Username: %s\'), $user_login) . "\\n";
         $message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\n";
         $message .= sprintf(__(\'If you have any problems, please contact me at %s.\'), get_option(\'admin_email\')) . "\\n";
         $message .= sprintf(__(\'Best Regards, %s\'), get_option(\'blogname\'));

         wp_mail($user_email, sprintf(__(\'[%s] Member Registration Email Verification\'), get_option(\'blogname\')), $message);

     }
 }
 // The callback function for the [cr] shortcode
 function cr_cb() {
   $fields = array();
   $errors = new WP_Error();

   // Buffer output
   ob_start();

   // Custom registration, go!
   cr($fields, $errors);

   // Return buffer
   return ob_get_clean();
 }
 add_shortcode(\'cr\', \'cr_cb\');
Question如何获取user\\u登录名、first\\u name和;第一个表单中的last\\u name条目填充第二个表单,而不丢失步骤1验证代码?如果可能,请提供一个示例。

1 个回复
最合适的回答,由SO网友:DigitalDesigner 整理而成

我已经找到了解决我的问题的办法。下面是我为解决这个问题所做的。这是一个有点绕圈子的过程,主要是分析什么时候出了问题。

第1步。确定我遇到的最大问题是将信息从一个页面移动到另一个页面。因此,我将两个表单放在了同一个页面中,添加了一个div样式来显示:第二个表单没有,将其隐藏在浏览器视图中。

第2步。既然两个表单都在同一页上,我将echo重定向编辑为表单1上成功提交表单的当前页面。

第3步。为了隐藏第一个表单并显示第二个表单,我在成功区域放置了一个样式回显,以便显示表单2和另一个样式回显以隐藏表单1。

对于访问者来说,表单可以无缝生成,并且表单1中的所有信息都可以正确地移动到表单2。

这是我编辑过的代码。

在“验证字段并生成错误”下

// Validate fields and produce errors
if (cv_validate($fields, $errors)) {

// If successful, display a message
        echo \'<style type="text/css">.step1-form{display:none !important;}</style>\';
        echo \'<style type="text/css">.step2-form{display:block !important;}</style>\';

}
这允许第一个表单在成功时隐藏,第二个表单显示。

我还将类添加到相应的表单和表单周围的div标记中,瞧,这两个插件彼此之间可以完美地通信,访问者不必在第二个表单上重新输入数据,这使得他们的注册过程更加简单。

我希望有人发现这对他们未来的项目有帮助。感谢社区对此主题的投入。

结束

相关推荐

Disallow second login session

我现在正在努力寻找一个解决方案,持续了两个月,但不幸的是,我没有找到任何结果。我有一个基于订阅的网站,可以播放音乐。客户基本上购买的是一个“面板”,他们在那里听音乐without the need to refresh 页面。我需要的是,如果他们的帐户已经在使用中,就不允许他们登录。我发现许多插件会强制用户注销,但只有在刷新页面时才会发生这种情况。例如,如果:A登录,它也可以工作。B使用相同的帐户登录,此时A将自动注销(我猜是使用Ajax)。过去没有人需要这个,这有点奇怪。有人能帮忙吗?提前谢谢。