Submit Form and create user - Worpress + Google Forms - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, August 24, 2022

Submit Form and create user - Worpress + Google Forms

I have a user registration form simple HTML no plugin and I would like to make it create a user in worpress (this part works) and submit the form as google form with no redirect (only the message displayed by the form). What can be the solutions?

I have tried to recreate the same form in google format and hidden, but the submit button does not submit and if it does the form was empty as it was submitted too late.

This part is the HTML form

function registration_form( $username, $password, $email, $first_name, $last_name ){
echo '
 <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
 <div>
 <label for="firstname">Jméno <strong>*</strong></label>
 <input type="text" name="fname" required id="name" value="' . ( isset( $_POST['fname']) ? $first_name : null ) . '">
 </div>
 
 <div>
 <label for="website">Příjmení <strong>*</strong></label>
 <input type="text" name="lname" required id="lastname" value="' . ( isset( $_POST['lname']) ? $last_name : null ) . '">
 </div>
 
   <div>
 <label for="email">Datum narození <strong>*</strong></label>
 <input type="date" name="bday" required id="bday" value="' . ( isset( $_POST['bday']) ? $birthday : null ) . '">
 </div>
 
  <div>
 <label for="email">E-mail <strong>*</strong></label>
 <input type="email" name="email" required id="email" value="' . ( isset( $_POST['email']) ? $email : null ) . '">
 </div>
 
   <div>
 <input type="text" name="email1" required id="email1" value="' . ( isset( $_POST['email1']) ? $email1 : null ) . '" placeholder="Potvrzení emailu">
 </div>
 
  <div>
 <label for="email">Telefon <strong>*</strong></label>
 <input type="text" name="telefon" required id="telefon" value="' . ( isset( $_POST['telefon']) ? $phone : null ) . '">
 
 <br><br><br><br>
 <div>
 <label for="username">Přihlašovací jméno <strong>*</strong></label>
 <input type="text" name="username" value="' . ( isset( $_POST['username'] ) ? $username : null ) . '" placeholder="Minimálně 4 znaky. Pouze písmena a čísla.">
 </div>
 
 <div>
 <label for="password">Heslo <strong>*</strong></label>
 <input type="password" name="password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '" placeholder="Minimálně 5 znaků">
 </div>

 <div align="center"><br>
 <input type="submit" name="submit" value="Registrovat se"/>
</div></form>
 ';
}

Checking for errors and validating form

function registration_validation( $username, $password, $email, $first_name, $last_name ) {

global $reg_errors;
 $reg_errors = new WP_Error;

//Check empty fields
 if ( empty( $username ) || empty( $password ) || empty( $email )) {
 $reg_errors->add('field', 'Prosím vyplňte povinné informace.');
 }

 //Check user name lenght greater than 4 character.
 if ( 4 > strlen( $username ) ) {
 $reg_errors->add( 'username_length', 'Přihlašovací jméno je příliš krátké: 4 znaky nejméně' );
 }

//Check user name is exists
 if ( username_exists( $username ) ){
 $reg_errors->add('user_name', 'Již existuje uživatel se stejným uživatelským jménem. Prosím, zvolte si jiné.');
 }

//validate wp user name
 if ( ! validate_username( $username ) ) {
 $reg_errors->add( 'username_invalid', 'Sorry, the username you entered is not valid' );
 }

//check password lengh 
 if ( 5 > strlen( $password ) ) {
 $reg_errors->add( 'password', 'Heslo musí mít nejméně 5 znaků.' );
 }

//check email is correct email
 if ( !is_email( $email ) ) {
 $reg_errors->add( 'email_invalid', 'Email není správný' );
 }
    
 // check email already in use or not
 if ( email_exists( $email ) ) {
 $reg_errors->add( 'email', 'Již existuje uživatel se stejným emailem.' );
 }


if ( is_wp_error( $reg_errors ) ) {
 foreach ( $reg_errors->get_error_messages() as $error ) {
 echo '<div class="zaloha">';
 echo '<strong>ERROR</strong>:';
 echo $error . '<br/>';
 echo '</div>'; 
 }
 }
}

function complete_registration() {
 global $reg_errors, $username, $password, $email, $first_name, $last_name
 if ( 1 > count( $reg_errors->get_error_messages() ) ) {
 $userdata = array(
 'user_login' => $username,
 'user_email' => $email,
 'user_pass' => $password,
 'first_name' => $first_name,
 'last_name' => $last_name,
 'nickname' => $nickname, 
 );

 $user = wp_insert_user( $userdata );
 echo '<div class = "zaloha">Vaše registrace proběhla úspěšně.</div><br>'; 
 }
}

function registration_function() {
global $reg_errors, $username, $password, $email, $first_name, $last_name;

if ( isset($_POST['submit'] ) ) {
 registration_validation(
 $_POST['username'],
 $_POST['password'],
 $_POST['email'],
 $_POST['fname'],
 $_POST['lname'],
 $_POST['nickname'] );
    
 
 // call @function complete_registration to create the user only when no WP_error is found
 complete_registration($username, $password, $email, $first_name, $last_name);
}
 
 registration_form( $username, $password, $email, $first_name, $last_name);
}

EDIT As suggested in the comments, I have used CUrl to automatically submit the form with a redirect once the form is submitted to not display the Google confirmation page.



source https://stackoverflow.com/questions/73449866/submit-form-and-create-user-worpress-google-forms

No comments:

Post a Comment